Which of the following is the worst case scenario for the Insertion sort algorithm, when the array needs to be sorted in ascending order?
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
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 , so the time taken is .
Option 1: This is the best case. Each element is compared only once, so the time is .
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.
Related questions:
2026: 24 May Shift 1
2025: 14 May Shift 1
2026: 24 May Shift 1
2026: 23 May Shift 1