Proofs: Odd-even merge sort
These are the human proofs of Odd-even merge sort, at full length.
Odd-even merge sort sorts each input full
This proof shows: correctness.
The calls of OEM-SORT and ODD-EVEN-MERGE make the same comparators on each input, so they form a comparator network of standard comparators. Each comparator only exchanges two items, so the output is a permutation of the input. By the zero-one principle it is enough to show that the output is sorted for each input of zeros and ones.
For a call ODD-EVEN-MERGE with , write for . This is the subsequence of the call. Its first half is and its second half is . Each comparator of the call has both wires in the subsequence, so the call changes no other entry of .
Lemma 1. Let be a sorted 0-1 sequence of even length . Then the items at even places hold zeros and are sorted, and the items at odd places hold zeros and are sorted.
Proof. A subsequence of a sorted sequence is sorted. The zeros are at places . Of these, are even and are odd.
Lemma 2 (merge). If the first half and the second half of the subsequence of a call ODD-EVEN-MERGE are sorted 0-1 sequences, then after the call the subsequence is sorted.
Proof. By induction on , which is a power of 2.
If , then , so line L7 fails and line L11 compares and . After it, .
Let and , which is even. Let the first half have zeros and the second half zeros. The call of line L8 has the subsequence of stride . Its first half is the even places of the first half of , and its second half is the even places of the second half of , because is even. By Lemma 1 both are sorted. So by the induction hypothesis, after line L8 the even places of are sorted and hold zeros. In the same way, the call of line L9 has the subsequence , and after it the odd places of are sorted and hold zeros. The two calls have disjoint wires, so the order of lines L8 and L9 does not matter. Now if and only if , and if and only if . Also .
- If , the zeros are at the places , so is sorted.
- If , the zeros are at the places , so is sorted.
- If , the zeros are at the places and at the place , and the place holds a 1. So is sorted except that and . Here and , because the even places hold at most zeros.
Line L10 runs over for while , and compares with . So it applies the comparators of the subsequence for . These pairs are disjoint. In the first two cases each pair is already in order, and nothing changes. In the third case the pair is one of them, with , and its comparator exchanges the 1 and the 0. Each other pair is in order. After line L10, is sorted in each case.
Theorem. For each power of 2 and each , OEM-SORT sorts for each 0-1 content of this part, and it changes no other entry of . So by the zero-one principle, OEM-SORT sorts each array of items from a totally ordered set.
Proof. By induction on . If , line L1 returns. Let and . By the induction hypothesis, lines L3 and L4 sort and and change nothing else. The call of line L5 has , so its subsequence is , and its two halves are sorted. By Lemma 2, after line L5 the part is sorted.