Skip to content
Official PYQ · 2021

GATE CS 2021 Set 1 Q9 — Searching, Sorting, Hashing

Explain with AI

Consider the following array.2332456972738997\begin{array}{|l|l|l|l|l|l|} \hline 23&32&45&69&72&73&89&97 \\ \hline\end{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?

Choose one option
View worked solution

Answer: C

The given array is already sorted in ascending order:

Array: [23,32,45,69,72,73,89,97](n=8)\text{Array: } [23, 32, 45, 69, 72, 73, 89, 97] \quad (n = 8)

Let us analyze the number of element comparisons required by each sorting algorithm on an already sorted array of size n=8n = 8:

  • Insertion Sort:
    For an already sorted array, each element from index 11 to n−1n-1 is compared exactly once with its immediate predecessor to confirm its correct position.
    Total comparisons=n−1=8−1=7\text{Total comparisons} = n - 1 = 8 - 1 = 7
  • Selection Sort:
    Selection sort always performs the same number of comparisons regardless of the initial order of the array.
    Total comparisons=n(n−1)2=8×72=28\text{Total comparisons} = \frac{n(n-1)}{2} = \frac{8 \times 7}{2} = 28
  • Mergesort:
    Mergesort uses a divide-and-conquer approach. For n=8n = 8, the array is split into subarrays and merged. In the best-case merging scenario (which occurs here), the number of comparisons is:
    Total comparisons=4×1 (level 1)+2×2 (level 2)+1×4 (level 3)=12\text{Total comparisons} = 4 \times 1 \text{ (level 1)} + 2 \times 2 \text{ (level 2)} + 1 \times 4 \text{ (level 3)} = 12
  • Quicksort (with last element as pivot):
    Since the array is already sorted, choosing the last element as the pivot always results in the most unbalanced partitions (worst-case behavior).
    Total comparisons=n(n−1)2=28\text{Total comparisons} = \frac{n(n-1)}{2} = 28

Comparing the results, Insertion Sort requires the least number of comparisons (77).

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Searching, Sorting, Hashing questions