Question

Consider the following array.

2332456972738997

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?

Choose one option
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 [23,32,45,69,72,73,89,97][23, 32, 45, 69, 72, 73, 89, 97] contains n=8n = 8 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 n(n−1)2=8×72=28\frac{n(n-1)}{2} = \frac{8 \times 7}{2} = 28 comparisons regardless of input order.
  • Mergesort: Recursively splits and merges subarrays, requiring Θ(nlog⁡2n)≈8×3=24\Theta(n \log_2 n) \approx 8 \times 3 = 24 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 (00 and n−1n-1 elements), requiring n(n−1)2=28\frac{n(n-1)}{2} = 28 comparisons.
  • Insertion sort: For an already sorted array, each element A[i]A[i] is compared exactly once with its immediate predecessor A[i−1]A[i-1] to confirm it is already in place. It performs only n−1=8−1=7n - 1 = 8 - 1 = 7 comparisons.

Because 7 is strictly less than 28 and 12, Insertion sort uses the least number of comparisons (option C).

Final answer

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.