GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Lomuto partition (pivot = last element, loop j from lo to hi−1 comparing a[j] ≤ pivot) is used in quicksort on [5, 2, 8, 1, 9, 3, 7, 4, 6], recursing on both sides until size ≤ 1. How many element-to-pivot comparisons are performed in total?

  1. A. 16
  2. B. 18
  3. C. 21
  4. D. 17

Correct answer: D. 17

Each partition call on m elements costs exactly m − 1 comparisons. Tracing the recursion, the calls have sizes 9, 5, 3, 2 and 3, costing 8 + 4 + 2 + 1 + 2 = 17.

More Algorithms questions

Exams that ask this topic