Proofs: Bitonic sort
These are the human proofs of Bitonic sort, at full length.
Bitonic sort sorts each input full
This proof shows: correctness.
The calls of BITONIC-SORT and HALF-CLEANERS make the same comparators on each input, so they form a comparator network. 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 in . In this proof all sequences are of zeros and ones, and positions are counted from 0.
Bitonic 0-1 sequences. Call bitonic when the set of positions with is empty or one cyclic interval of , with the positions read modulo . The complement of a cyclic interval is empty or a cyclic interval, so is bitonic exactly when its zeros fill one cyclic interval or none. Each sorted sequence is bitonic, and so is a sequence of one value only, which we call clean. The reverse of a bitonic sequence is bitonic. These are the 0-1 images of the bitonic sequences of the usual definition.
Lemma 1 (half-cleaner). Let be bitonic, and apply the comparators for (line L10, with ). Let be the first half and the second half of the result. Then and are bitonic, or is clean, and for all .
Proof. The comparators have disjoint wires, so and . Let be the number of ones of .
Case . The ones fill a cyclic interval of length . The positions and have cyclic distance in both directions, so an interval that holds both has at least positions. So no has , and is all zeros. Next, if and only if or , that is, if and only if for some . The map sends the consecutive positions of to consecutive positions of , so the ones of fill one cyclic interval. So is bitonic, is clean, and .
Case . Then has zeros, and they fill a cyclic interval. The argument above with zeros and ones exchanged shows that no has , so is all ones, and the zeros of fill one cyclic interval. So is bitonic, is clean, and .
Lemma 2. For each power of 2 and each , HALF-CLEANERS sorts if this part is a bitonic 0-1 sequence, and it changes no other entry of .
Proof. By induction on . If , line L8 returns and one item is sorted. Let . Line L10 is the half-cleaner of Lemma 1 on the part. After it, both halves are bitonic and each item of the first half is at most each item of the second half. By the induction hypothesis, lines L11 and L12 sort each half and touch nothing else. A sorted first half whose items are all at most the items of a sorted second half gives a sorted part.
Lemma 3 (the flip). Let with be two sorted 0-1 halves. Then after line L5, both halves are bitonic and each item of the first half is at most each item of the second half.
Proof. Take . Let and , and let be in reverse order. The sequence is bitonic, because its ones are consecutive. Line L5 compares with , and . So the first half after line L5 is the first half of the half-cleaner applied to , and the second half after line L5 is the second half of that half-cleaner, in reverse order. By Lemma 1, and are bitonic and for all . The reverse of is bitonic and holds the same items as .
Theorem. For each power of 2 and each , BITONIC-SORT sorts for each 0-1 content of this part, and it changes no other entry of . So, by the zero-one principle, BITONIC-SORT sorts each array of items from a totally ordered set.
Proof. By induction on . If , line L1 returns. Let . By the induction hypothesis, lines L3 and L4 sort the two halves and change nothing else. By Lemma 3, after line L5 both halves are bitonic and each item of the first half is at most each item of the second half. By Lemma 2, lines L6 and L7 sort each half, and the part is then sorted. Each line touches only positions in .