GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Quicksort partitions m elements with exactly m − 1 comparisons and then recurses on the two sides (sizes k and m − 1 − k). Over all inputs and pivot choices, what is the minimum possible total number of comparisons for n = 20?

  1. A. 58
  2. B. 50
  3. C. 66
  4. D. 54

Correct answer: D. 54

Let B(m) = m − 1 + min_k [B(k) + B(m − 1 − k)], B(0) = B(1) = 0. Balanced splits are best: B(3) = 2, B(4) = 4, B(7) = 10, B(9) = 16, B(10) = 19, B(15) = 34, and B(20) = 19 + B(9) + B(10) = 19 + 16 + 19 = 54. 58 is B(21).

More Algorithms questions

Exams that ask this topic