Skip to content
Official PYQ · 1996

GATE CS 1996 Q2.15 — Searching, Sorting, Hashing

Explain with AI

Quick-sort is run on two inputs shown below to sort in ascending order taking first element as pivot

  1. 1,2,3,…n1, 2, 3, \dots n
  2. n,n−1,n−2,…,2,1n, n-1, n-2, \dots, 2, 1

Let C1C_1 and C2C_2 be the number of comparisons made for the inputs (i) and (ii) respectively. Then, 

Choose one option
View worked solution

Answer: C

In Quick-sort, when the first element is chosen as the pivot:

  1. Input (i) is already sorted: 1,2,3,…,n1, 2, 3, \dots, n
    The pivot is the smallest element (11). Partitioning requires comparing the pivot with the remaining n−1n-1 elements. This splits the array into an empty left subarray and a right subarray of size n−1n-1.
    The recurrence relation for the number of comparisons is: T(n)=T(n−1)+(n−1)T(n) = T(n-1) + (n-1)
  2. Input (ii) is reverse sorted: n,n−1,n−2,…,1n, n-1, n-2, \dots, 1
    The pivot is the largest element (nn). Partitioning requires comparing the pivot with the remaining n−1n-1 elements. This splits the array into a left subarray of size n−1n-1 and an empty right subarray.
    The recurrence relation for the number of comparisons is: T(n)=T(n−1)+(n−1)T(n) = T(n-1) + (n-1)

Since both cases result in the same highly unbalanced partition at every recursive step (splitting into sizes 00 and k−1k-1 for an array of size kk), the total number of comparisons is identical:

C1=C2=n(n−1)2=Θ(n2)C_1 = C_2 = \frac{n(n-1)}{2} = \Theta(n^2)

Therefore, C1=C2C_1 = C_2.

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

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