Skip to content
Official PYQ · 2009

GATE CS 2009 Q39 — Searching, Sorting, Hashing

Explain with AI

In quick-sort, for sorting nn elements, the (n/4)th\left(n/4\right)^{th} smallest element is selected as pivot using an O(n)O(n) time algorithm. What is the worst case time complexity of the quick sort?

Choose one option
View worked solution

Answer: B

In the given quick-sort variant, the pivot is always chosen as the (n/4)th\left(n/4\right)^{\text{th}} smallest element using an O(n)O(n) time selection algorithm. This guarantees a consistent split ratio of at least 1:31:3 at every step of the recursion.

The recurrence relation for the worst-case time complexity T(n)T(n) is:

T(n)=T(n4)+T(3n4)+Θ(n)T(n) = T\left(\frac{n}{4}\right) + T\left(\frac{3n}{4}\right) + \Theta(n)

Where:

  • T(n4)T\left(\frac{n}{4}\right) and T(3n4)T\left(\frac{3n}{4}\right) represent the time to recursively sort the two partitioned subarrays.
  • Θ(n)\Theta(n) is the time taken to find the (n/4)th(n/4)^{\text{th}} smallest element and partition the array around it.

We can solve this recurrence using the recursion tree method:

  1. At the root (level 0), the cost is cncn.
  2. At level 1, the subproblems have sizes n4\frac{n}{4} and 3n4\frac{3n}{4}. The total cost is c(n4)+c(3n4)=cnc\left(\frac{n}{4}\right) + c\left(\frac{3n}{4}\right) = cn.
  3. At any level ii, the sum of the sizes of all subproblems is at most nn, meaning the total work done at each level is bounded by cncn.
  4. The recursion stops when the subproblem size reduces to 11. The longest path in the tree corresponds to repeatedly taking the larger fraction 34\frac{3}{4}, which gives a maximum depth of: log⁡4/3n=Θ(log⁡n)\log_{4/3} n = \Theta(\log n)

Multiplying the work per level by the maximum depth of the tree:

T(n)≤cn×log⁡4/3n=Θ(nlog⁡n)T(n) \le cn \times \log_{4/3} n = \Theta(n \log n)

Since the depth is at least log⁡4n=Θ(log⁡n)\log_{4} n = \Theta(\log n), we also have T(n)≥Θ(nlog⁡n)T(n) \ge \Theta(n \log n). Thus, the worst-case time complexity is Θ(nlog⁡n)\Theta(n \log n).

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

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