Skip to main content

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…

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?

  1. A
    Selection sort
  2. B
    Mergesort
  3. C
    Insertion sort
    Correct answer
  4. D
    Quicksort 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.