Consider the following array. Which algorithm out of the following options uses the least number of comparisons (among the array elements) to sort the above array in ascending order?
GATE CSE 2021Algorithms › Sorting AlgorithmsVerified
GATE CSE 2021 PYQ: Consider the following array. 23 32 45 69 72 73 89 97 \begin{array}{|l|l|l|l|l|l|} \hline…
- ASelection sort
- BMergesort
- CInsertion sortCorrect answer
- DQuicksort using the last element as pivot
Answer
(C) Insertion sort
Approach
The array 23, 32, 45, 69, 72, 73, 89, 97 is already sorted in ascending order. The correct answer is whichever algorithm does the least work on an already-sorted input. So we compare the number of element comparisons each option makes on a sorted array of n = 8 elements.
Since the array is already sorted ascending, insertion sort is in its best case and needs only n - 1 = 7 comparisons, fewer than mergesort's Θ(n log n) and far fewer than the Θ(n²) cost of selection sort and quicksort-with-last-pivot on this input. Hence option C.
Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.