Question:

Consider that the quick sort algorithm is used to sort an array of \(n\) distinct randomly ordered elements. In every call, the pivot is chosen as the first element of the current subarray.
Let \(T(n)\) denote the expected time to sort the array. Assume that the time to partition is linear in the size of the current subarray.
Which of the following recurrence relations correctly represents \(T(n)\) in this scenario?

Show Hint

Random input means the pivot's rank after partitioning is uniformly distributed over 0 to n-1, so average \(T(k)+T(n-1-k)\) over all k.
Updated On: Jul 22, 2026
  • \(T(n) = T(1) + T(n-1) + O(n)\)
  • \(T(n) = T\left(\dfrac{n}{4}\right) + T\left(\dfrac{3n}{4}\right) + O(n)\)
  • \(T(n) = 2T\left(\dfrac{n}{2}\right) + O(n)\)
  • \(T(n) = \dfrac{1}{n}\displaystyle\sum_{k=0}^{n-1} \left[T(k) + T(n-k-1)\right] + O(n)\)
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is D

Solution and Explanation

Step 1: Understand what makes this the expected time, not the worst case.
The array is randomly ordered, and the pivot is always taken as the first element of whatever subarray we are currently sorting. Because the input order is random, we cannot assume the pivot always lands in a fixed spot like the smallest or the middle element, instead, the pivot could equally likely end up being the smallest, the largest, or anything in between, once we finish partitioning.

Step 2: Work out the distribution of the pivot's final rank.
After partitioning around the pivot, suppose \(k\) elements end up smaller than the pivot (forming the left subarray of size \(k\)) and the remaining \(n-1-k\) elements end up larger (forming the right subarray of size \(n-1-k\)). Since the array is randomly ordered, the first element is equally likely to be any of the \(n\) elements in terms of rank, so \(k\) is equally likely to be any value from 0 up to \(n-1\), each with probability \(\frac{1}{n}\).

Step 3: Build the recurrence by averaging over all possible splits.
Given a particular value of \(k\), the cost is \(T(k) + T(n-1-k)\) for sorting the two resulting parts, plus \(O(n)\) for the partition step itself (given as linear in the current subarray size). Since every split value \(k\) from 0 to \(n-1\) is equally likely, the expected time is the average of \(T(k) + T(n-1-k)\) over all these \(n\) equally likely outcomes, plus the partition cost:
\[ T(n) = \frac{1}{n}\sum_{k=0}^{n-1}\left[T(k) + T(n-k-1)\right] + O(n) \]
This matches option (D) exactly.

Step 4: Why the other options fail.
Option (A), \(T(n) = T(1) + T(n-1) + O(n)\), describes the worst case, where the pivot always ends up smallest or largest, giving one part of size 1 every single time, this happens only for already sorted or specially adversarial inputs, not for a randomly ordered array. Options (B) and (C) both assume a single fixed split ratio, one quarter-three quarters, or half-half, happens on every single call. But with a random array and a first-element pivot, the actual split ratio varies from call to call, we cannot pin it to one fixed ratio, we must average across every possible ratio, which is exactly what option (D) does.

Step 5: Final Answer.
The correct recurrence for the expected time is option (D). \[ \boxed{T(n) = \frac{1}{n}\sum_{k=0}^{n-1}\left[T(k)+T(n-k-1)\right] + O(n)} \]
Was this answer helpful?
0
0

Top GATE DA Data Science and Artificial Intelligence Questions

View More Questions

Top GATE DA Data Structures and Algorithms Questions