GATE Computer ScienceHardGATE CS

The array [7, 3, 11, 1, 9, 12, 5, 2, 10, 4, 8, 6] is sorted by merge sort. How many inversions (pairs i < j with a[i] > a[j]) does it contain, i.e. how many cross-pair swaps would the inversion-counting merge step add up to?

  1. A. 36
  2. B. 31
  3. C. 35
  4. D. 33

Correct answer: D. 33

Counting while merging (each time an element of the right half is placed before k remaining left elements, add k) totals 33. A brute-force check over all 66 pairs agrees.

More Algorithms questions

Exams that ask this topic