Proofs: Counting inversions by merge sort
These are the human proofs of Counting inversions by merge sort, at full length.
Sort-and-Count returns the number of inversions and sorts the range full
This proof shows: correctness, termination.
We use the pseudocode of the page. For a range , write for the number of pairs of indices with , taken at the start of the call. For two finite multisets and of items, write , counting each copy of an item separately. depends only on the two multisets, not on the order in which they are stored.
Split of the count. Let , let be the multiset of and the multiset of . Each pair of the range has both indices below , both indices at least , or . The pairs of the third kind with are exactly the pairs of with the first item larger. So
Lemma (MERGE-AND-COUNT). Assume that and that and are sorted, with multisets and . Then MERGE-AND-COUNT returns , leaves sorted with the multiset , and changes no other item of .
Proof. The data movement of lines L6 to L13 is the merge of merge sort, and the counter does not affect it. So the proof of the merge lemma on the merge sort page shows that the range ends sorted with the multiset , that no other item changes, and that the write position never passes the read position . It remains to show what counts. Let , and for write for the item at index at the start of the call; these items are not changed before they are read. We use this invariant of the loop of line L8.
(C) , and each item of is at most each of .
Initialization. Line L7 sets , and . The sum is empty and is empty, so (C) holds.
Maintenance. Assume (C) before a step of line L8, so and .
- If , line L9 places and increases . The right half is sorted, so , and the second part of (C) holds for . The values and do not change, so the first part still holds.
- If , line L10 places , adds to , and increases . Since is sorted, for each , so at least items of are larger than . By the second part of (C), each with is at most . So exactly items of are larger than , and the new is the sum of (C) up to . The second part holds for the new , because are a subset of the old list.
Each step increases by exactly 1, and , so the loop stops after at most steps.
Termination. The loop stops when or .
- If , the first part of (C) is the sum over all right items, which is because holds the multiset . The loop of line L12 only moves items and does not change .
- If and , the second part of (C) says that each item of is at most each of . So these right items have no larger left item, and their terms of are 0. The sum of (C) up to is then . The loop of line L12 does not run, since .
In both cases line L14 returns .
Theorem. SORT-AND-COUNT returns , leaves sorted with the same multiset of items, changes no other item of , and terminates.
Proof. By strong induction on .
Base. If , the range has no pair of indices, so its inversion count is 0. Line L1 returns 0 and changes nothing, and a range with at most one item is sorted.
Step. Let and assume the claim for all shorter ranges. Line L2 gives , so the calls of lines L3 and L4 are on ranges of lengths and , both less than . By the induction hypothesis, line L3 returns , sorts that part and changes nothing else; in particular the right half is not changed. Line L4 then returns for the original right half and sorts it, without changing the left half. Both halves keep their multisets and . By the lemma, MERGE-AND-COUNT returns and leaves the range sorted with multiset . By (1), line L5 returns . Each call is on a strictly shorter range, so the recursion terminates.
Repeated keys. The proof never assumes distinct items. An equal pair goes to line L9, and it is not counted, which matches the strict test in the definition of an inversion.