edited by
19,807 views
70 votes
70 votes

In a permutation $a_1\ldots a_n$, of $n$ distinct integers, an inversion is a pair $(a_i, a_j)$ such that $i < j$ and $a_i > a_j.$

What would be the worst case time complexity of the Insertion Sort algorithm, if the inputs are restricted to permutations of $1. . . n$ with at most $n$ inversions?

  1. $\Theta(n^2)$
  2. $\Theta(n\log n)$
  3. $\Theta(n^{1.5})$
  4. $\Theta(n)$
edited by

9 Answers

0 votes
0 votes
Looking at the code of insertion sort we can see that we require one comparison for all ‘n’ elements initially. (yes for the first element we don’t need this, but let us ignore it.). Therefore ‘n’ comparisons.

Then for each swap(= number of inversions, let it be ‘d’), there will be one comparison each to check whether we need to swap or not. Therefore ‘d’ more comparisons.

Obviously ‘d’ swaps for ‘d’ inversions. Therefore O(n+d+d) = O(n+d).

Here d=n, therefore O(n).
Answer:

Related questions