Skip to main contentSkip to solution

What is the time complexity of insertion sort in worst case?

Solution

✅ Correct Option: 4

In the worst case the array is in reverse order. Each new element must be compared with, and shifted past, every element already sorted before it.

The ii-th element needs ii shifts, so the total is:

1+2+⋯+(n−1)1 + 2 + \dots + (n-1)

=n(n−1)2= \dfrac{n(n-1)}{2}

=O(n2)= \boxed{O(n^2)}


Option 3: O(n)O(n) is the best case, when the array is already sorted and each element needs only one comparison.

Option 2: O(nlog⁡2n)O(n\log_2 n) is the worst case of merge sort and heap sort, not insertion sort.

Keyboard Shortcuts

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