GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Top-down merge sort splits n elements into a left part of ⌈n/2⌉ and a right part of ⌊n/2⌋, and its merge stops comparing as soon as one part is exhausted (then copies the rest). How many key comparisons does it make on an input of 100 distinct keys that is already sorted in increasing order?

  1. A. 316
  2. B. 356
  3. C. 573
  4. D. 332

Correct answer: B. 356

On sorted input every left element is smaller than every right element, so a merge makes exactly |left| = ⌈m/2⌉ comparisons (not min(n₁, n₂)). Summing ⌈m/2⌉ over the recursion: C(100) = 2C(50) + 50, C(50) = 2C(25) + 25, C(25) = C(13) + C(12) + 13, … gives 356. Using ⌊m/2⌋ gives 316 (that is what reverse-sorted input costs); 573 is the worst case.

More Algorithms questions

Exams that ask this topic