Which one of the following in place sorting algorithms needs the minimum number of swaps?
View worked solution
Answer: C
The correct option is C.
To determine which in-place sorting algorithm requires the minimum number of swaps, let us analyze the swap complexity of each algorithm for an array of size :
- Selection Sort: In each of the passes, the algorithm finds the minimum element from the unsorted part of the array and swaps it into its correct position. Therefore, it performs at most swaps in the worst, average, and best cases. Its swap complexity is strictly .
- Insertion Sort: Typically shifts elements rather than swapping. However, if implemented using swaps, it requires swaps in the worst case and on average.
- Quick Sort: In the partition step, elements are swapped to position them relative to the pivot. The worst-case number of swaps is and the average-case is .
- Heap Sort: During the heapify and deletion phases, elements are repeatedly swapped. It requires swaps in both the average and worst cases.
Since is asymptotically smaller than and , Selection Sort guarantees the minimum number of swaps among the given options.
Free practice · Your answers on this page are not saved.
