Proofs: In-place merge sort
These are the human proofs of In-place merge sort, at full length.
In-place merge sort returns a stable sorted order full
This proof shows: correctness, termination.
Tag each item with its input position. A sequence is stably sorted if it is in non-decreasing order of keys and items with equal keys are in increasing order of their tags. We write for "not ", that is, the key of is at most the key of .
Lemma 1 (ROTATE). ROTATE replaces , with and , by , and changes nothing else.
Proof. If or is empty, and line L16 does nothing. Otherwise, write for reversed. The first two reversals give , and the third gives .
Lemma 2 (the searches). On a sorted range, LOWER-BOUND returns the least such that each item of satisfies not , and each item of is less than . UPPER-BOUND returns the least such that each item of satisfies not , and each item of is greater than .
Proof. This is binary search on a monotone predicate: the invariant is that every item before has the property of the left part and every item from on has the property of the right part. Each step keeps it and shrinks , and the loop ends with at the boundary.
Lemma 3 (MERGE-IN-PLACE). Let and be stably sorted, with every tag in smaller than every tag in . Then MERGE-IN-PLACE ends, changes only , and leaves there the stably sorted order of the items of and .
Proof. By induction on . If one run is empty (line L6), the other run is the answer. If (line L7), there is one item in each run, and the exchange happens exactly when the right item has the smaller key, so equal keys keep the left item first.
Let , with both runs non-empty.
Case 1: (line L9). Then , so . Let . Split at and at . By Lemma 2, every item of is less than , and no item of is less than . As is sorted, every item of is and every item of . After line L12, by Lemma 1, the range holds , and line L13 sets to the boundary between and .
The two calls of lines L14 and L15 get runs and , each run stably sorted as a part of a stably sorted run, and each with left tags smaller than right tags. They are smaller than : the first has items, and the second lacks the items of , which is not empty. So by induction they produce the stably sorted orders of and of .
It remains to show that is stably sorted, that is, for and , belongs before . Every such is and every such has , so . If the keys of and are equal, then both equal the key of . Then , because the items of are less than , so . If , comes before in the stably sorted run , so its tag is smaller. If , the tag of is smaller than that of . In both cases belongs before .
Case 2: (line L11). Then , so . Let , the first item of . By Lemma 2, every item of is , and every item of is greater than . As is sorted, every item of is every item of . The range becomes as before. The first call lacks and the second lacks the items of , which is not empty, so both are smaller than . For and we get . If their keys are equal, both equal the key of , so , and . If , it is before in the stably sorted run ; if , its tag is smaller than any tag in . So belongs before .
In both cases the result is stably sorted, and only was changed.
Theorem. IN-PLACE-MERGE-SORT ends and leaves stably sorted.
Proof. By induction on , the call on ends and stably sorts that range, changing nothing else. If , line L1 returns, and the range is already stably sorted. Otherwise , so both calls of lines L3 and L4 are on shorter ranges, and by induction they stably sort and . Since neither call moves an item out of its half, every tag in the left half is smaller than every tag in the right half. By Lemma 3, line L5 ends and leaves the stably sorted order of the whole range.