Skip to main contentSkip to solution

Which of the following is the worst case scenario for the Insertion sort algorithm, when the array needs to be sorted in ascending order?

Solution

✅ Correct Option: 2

In insertion sort, each new element is moved left past every larger element in the sorted part. If the array is in descending order, every element is smaller than all the ones before it, so it must be compared with and shifted past all of them.

The total number of shifts is 1+2+⋯+(n−1)=n(n−1)21 + 2 + \dots + (n-1) = \dfrac{n(n-1)}{2}, so the time taken is O(n2)\boxed{O(n^2)}.


Option 1: This is the best case. Each element is compared only once, so the time is O(n)O(n).

Option 3: A nearly sorted array needs very few shifts, so it is close to the best case.

Option 4: Duplicates do not add work, because insertion sort stops as soon as it meets an element that is not greater than the one being placed.

Keyboard Shortcuts

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