GATE / CS ARCHIVE

GATE CSE 2006 / QUESTION 52

Quicksort with linear-time median selection

Algorithms · Searching, Sorting & Selection · Full GATE CSE 2006 paper

GATE 2006, Q52 2 marks · MCQ
Report issue

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