GATE / CS ARCHIVE

SIMILAR QUESTIONS / ALGORITHMS

Solve quicksort with guaranteed constant-fraction pivots

2 questions solved the same way, newest first. Back to Algorithms

How these are solved

Linear-time selection chooses a pivot of specified rank, guaranteeing both recursive parts are constant fractions of n. Sum linear partition/selection cost across logarithmically many levels.

2 marks · MCQ
Report issue

Quicksort with a fixed-rank fractional pivot

Searching, Sorting & Selection

Question 39, GATE 2009: Quicksort selects the n/4-th smallest key as pivot using a linear-time selection algorithm. What is its worst-case time? A Θ(n); B Θ(n log n); C Θ(n²); D Θ(n² log n).
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Selecting the one-quarter order statistic in linear time gives recurrence T(n)=T(n/4)+T(3n/4−1)+Θ(n). Each recursion level has linear total work, and the longest/shortest nontrivial paths are logarithmic up to constants. Equivalently the Akra–Bazzi exponent is 1 because 1/4+3/4=1. Thus Θ(n log n), B, assuming rank partitioning is implemented appropriately for distinct keys or duplicates.

At each partition, linear-time selection plus partitioning costs Θ(n), and subproblems have sizes approximately n/4 and 3n/4. Both are fixed fractions, so recursion depth is Θ(log n); total size across a complete level remains proportional to n until leaves. Consequently total cost is Θ(n log n). Choosing the quarter-rank pivot is not perfectly balanced, but still prevents the n−1 versus 0 split responsible for quadratic quicksort.

Original paper, page 9

2 marks · MCQ
Report issue

Quicksort with linear-time median selection

Searching, Sorting & Selection

Question 52, GATE 2006: The median of n elements can be selected in O(n) time. What is quicksort’s complexity if the median is always chosen as pivot, with balanced partitioning?
Options:
(A) Θ(n)
(B) Θ(n log n)
(C) Θ(n²)
(D) Θ(n³)
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Select the median in Θ(n) time and partition in Θ(n) time, producing two subproblems of approximately n/2 elements. The recurrence T(n)=2T(n/2)+Θ(n) gives Θ(n log n), option B, including the median-selection cost.

2. This assumes standard balanced handling of duplicate keys or distinct keys; a two-way partition that mishandles equal keys can defeat that guarantee.

3. Teaching check: Finding the median is not free, but its linear cost can be absorbed into the same Θ(n) term as partitioning. Each recursion level processes a total of Θ(n) elements, and balanced halves give Θ(log n) levels. Thus median selection does not make quicksort linear overall.

Original paper, page 7