Count Inversions
Imagine you made a playlist and your friend rearranged it. How different is their version from yours? One way to measure that: count how many pairs of songs are in the wrong relative order. If song A came before song B in your original, but B is now before A — that's one inversion. The more inversions, the more shuffled things got.
Formally, an inversion is a pair of positions (i, j) where i < j but A[i] > A[j] — two elements are out of their natural order. A fully sorted array has 0 inversions. A completely reversed array has the maximum: n(n−1)/2 inversions (every possible pair is out of order).
The naive approach — too slow
Check every pair (i, j) with i < j. If A[i] > A[j], count it. Simple, but \(O(n^2)\) — for a million songs, that's a trillion comparisons.
The clever insight: count during merge sort
Here's the aha moment: merge sort's merge step already detects inversions — it just doesn't count them yet.
When merging two sorted halves [Left] and [Right], if we pick an element from Right because it's smaller than the current Left element — every remaining element in Left is larger than it and would have appeared before it in the original array. Each one of those is an inversion with the Right element we just picked.
So: when you copy from Right, add (number of remaining Left elements) to the inversion count. This piggybacks on merge sort at zero extra asymptotic cost — still \(O(n \log n)\) total.
Example walkthrough
Array: [3, 1, 2]. Inversions are (3,1) and (3,2) — count = 2.
Merge sort splits into [3] and [1, 2]. Left=[3], Right=[1,2]. First, we pick 1 from Right (1 < 3) — add 1 remaining Left element → count += 1. Then pick 2 from Right (2 < 3) — add 1 remaining Left element → count += 1. Total = 2. Correct!