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 ?
Show the worked solutionInsertion sort runs in \(O(n+I)\), where \(I\) is the number of inversions. A sorted list followed by a few (say \(k\)) random elements has at most about \(kn\) inversions, so insertion sort takes about \(O(kn)\), nearly linear for small \(k\).(A) Bubble sort makes many passes and swaps, slower in practice — incorrect.(B) Selection sort always takes \(\Theta(n^2)\) comparisons — incorrect.(C) Quick sort with a first or last pivot is at its worst, \(O(n^2)\), on nearly sorted input — incorrect.Answer: D
GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com