You have to sort a list L, consisting of a sorted list followed by a few…
2014
You have to sort a list L, consisting of a sorted list followed by a few ‘random’ elements. Which of the following sorting method would be most suitable for such a task ?
- A.
Bubble sort
- B.
Selection sort
- C.
Quick sort
- D.
Insertion sort
Attempted by 373 students.
Show answer & explanation
Correct answer: D
Correct answer: Insertion sort
Why insertion sort is most suitable:
Insertion sort is adaptive: it builds a sorted prefix and inserts each new element into its correct position by shifting elements.
Performance depends on the number of inversions (misplaced pairs). For a nearly-sorted list the number of inversions is small, so runtime is close to linear.
Best-case time complexity is O(n) when the list is already sorted; average/worst-case is O(n^2) but that is not reached for mostly-sorted inputs.
Insertion sort is in-place and stable and has low overhead, making it efficient for small numbers of random elements appended to an already-sorted list.
Short comparison with other methods:
Bubble sort: An optimized bubble sort can detect a fully sorted list, but it tends to perform many swaps and passes and is usually slower than insertion sort on nearly-sorted data.
Selection sort: Not adaptive; always performs O(n^2) comparisons regardless of initial order, so it does not exploit the existing sorted portion.
Quick sort: Good for random data on average, but it is not adaptive to existing order and has higher overhead for small perturbations; it can also degrade on already-sorted inputs without careful pivot selection.
Summary: For a list that is largely sorted with a few random elements, insertion sort minimizes work by only moving the misplaced elements and is therefore the most suitable choice.