Skip to main contentSkip to solution

Time Complexity of Insertion Sort having 'n' elements in the list in the worst case is:

Solution

✅ Correct Option: 3

In the worst case the list is in reverse order, so each new element must be compared with and shifted past all the elements already sorted before it.

The ii-th element needs i−1i - 1 shifts, so the total work is:

1+2+3+⋯+(n−1)=n(n−1)2=O(n2)1 + 2 + 3 + \dots + (n-1) = \dfrac{n(n-1)}{2} = \boxed{O(n^2)}


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

Insertion sort is O(n2)O(n^2) in both the average and the worst case.

Keyboard Shortcuts

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