Skip to main contentSkip to solution

In insertion sort, if we are given to sort a list of n elements, then how many comparisons will be required?

Solution

✅ Correct Option: 1,2

Insertion sort takes each element and compares it with the sorted part on its left, so the count depends on the input order. The number of comparisons is of order nn in the best case and of order n2n^2 in the worst case, so both Option 1 and Option 2 are accepted.

Best case (already sorted list): each of the n−1n-1 elements needs just one comparison, so the total is n−1≈nn-1 \approx n.

Worst case (reverse-sorted list): the kk-th element is compared with all kk elements before it.

1+2+3+⋯+(n−1)=n(n−1)2≈n21 + 2 + 3 + \dots + (n-1) = \dfrac{n(n-1)}{2} \approx \boxed{n^2}


Option 3: no case of insertion sort needs n3n^3 comparisons; the maximum is about n22\dfrac{n^2}{2}.

Option 4: log⁡2n\log_2 n comparisons is the cost of one binary search, not of sorting the whole list. Even binary insertion sort needs about nlog⁡2nn \log_2 n comparisons in total.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question