Question
Consider the following array.
| 23 | 32 | 45 | 69 | 72 | 73 | 89 | 97 |
|---|---|---|---|---|---|---|---|
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?
Official answer and solution
- Official answer
- C
- From
- Official GATE 2021 answer key
Solution
Examine the given array to observe that it is already sorted, and compute the number of comparisons for each candidate algorithm in its best case.
The given array contains elements in strictly ascending sorted order.
Let us evaluate the number of element comparisons made by each algorithm:
- Selection sort: Always finds the minimum of remaining elements, performing comparisons regardless of input order.
- Mergesort: Recursively splits and merges subarrays, requiring comparisons (at least 12 in the best case).
- Quicksort using last element as pivot: On a sorted array, picking the maximum element as pivot produces worst-case partitioning ( and elements), requiring comparisons.
- Insertion sort: For an already sorted array, each element is compared exactly once with its immediate predecessor to confirm it is already in place. It performs only comparisons.
Because 7 is strictly less than 28 and 12, Insertion sort uses the least number of comparisons (option C).
C
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2021 Computer Science and Information Technology (CS, Set-1)
- Question
- 19 of 65, printed as CS Q.9
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.