Skip to content
Official PYQ · 2006

GATE CS 2006 Q52 — Searching, Sorting, Hashing

Explain with AI

The median of nn elements can be found in O(n)O(n) time. Which one of the following is correct about the complexity of quick sort, in which median is selected as pivot?

Choose one option
View worked solution

Answer: B

When the median of nn elements is selected as the pivot in Quick Sort, the array is always partitioned into two equal (or nearly equal) halves of size approximately ⌊n/2⌋\lfloor n/2 \rfloor.

The recurrence relation for the time complexity T(n)T(n) is given by:

T(n)=2T(n2)+Time to find median+Partitioning timeT(n) = 2T\left(\frac{n}{2}\right) + \text{Time to find median} + \text{Partitioning time}

We are given that the median can be found in O(n)O(n) time. The partitioning step also takes O(n)O(n) time. Therefore, the recurrence relation simplifies to:

T(n)=2T(n2)+Θ(n)T(n) = 2T\left(\frac{n}{2}\right) + \Theta(n)

Applying the Master's Theorem for divide-and-conquer recurrences of the form T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n):

  • a=2a = 2, b=2b = 2
  • nlog⁡ba=nlog⁡22=n1=nn^{\log_b a} = n^{\log_2 2} = n^1 = n
  • Since f(n)=Θ(n)f(n) = \Theta(n), we fall into Case 2 of the Master's Theorem.

Thus, the solution to the recurrence is:

T(n)=Θ(nlog⁡n)T(n) = \Theta(n \log n)

Hence, the correct option is B.

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

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