Data Structures & AlgorithmsVery hardTCS Digital · Infosys SP

Top-down merge sort (split at mid = n // 2, recurse, then merge, stopping comparisons when one side runs out) sorts [14, 8, 20, 57, 38, 4, 35, 54]. How many key comparisons does it make?

  1. A. 16
  2. B. 24
  3. C. 15
  4. D. 17

Correct answer: A. 16

Count comparisons in every merge: a merge of sizes p and q costs between min(p, q) and p + q − 1 comparisons, depending on how early one side is exhausted. Here they total 16 (the worst case for n = 8 is 17).

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic