Searching, Sorting & Selection
![Question 9, GATE 2021: For [23,32,45,69,72,73,89,97], which standard sorting algorithm uses the fewest comparisons between array elements to sort in increasing order? A selection sort; B mergesort; C insertion sort; D quicksort with last-element pivot.](../../assets/questions/CS-2021-S1-Q19-1.webp)
Answer & solution
Official answer: C
Step through it (4 runs)
The displayed eight elements are already sorted. Standard insertion sort makes one failed element comparison for each of the seven later elements, totaling 7. Selection sort makes 28; ordinary mergesort makes more than 7; last-element-pivot quicksort repeatedly chooses the largest element and makes 28. Therefore insertion sort uses the least, C, for the standard versions of the named algorithms.
For selection sort the unsorted suffixes have lengths 8,7,...,2, producing 7+6+...+1=28 comparisons. Balanced top-down mergesort uses 4 comparisons merging singletons, 4 merging the sorted pairs, then 4 merging the two sorted halves, totaling 12 on this input. Insertion sort compares each new key with its immediate predecessor and stops, totaling 7. Quicksort's last pivot is always the maximum, so its partition costs sum to 28 in the standard implementation.