Proofs: Pairwise sorting network
These are the human proofs of Pairwise sorting network, at full length.
The pairwise network sorts each input full
This proof shows: correctness.
The calls of PAIRWISE-SORT make the same comparators on each input, so they form a network of standard comparators, and 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.
Fix a call PAIRWISE-SORT with items in its subsequence, and let be the number of pairs. Write for the item on the wire and for the item on the wire , for . The subsequence of the call is . Each comparator of the call has both wires in this subsequence.
Definition. For integers , the subsequence is in pair form when if and only if , and if and only if . Its amplitude is . In pair form the subsequence is . So it is sorted if and only if .
Lemma 1 (one round). Let with , and let the subsequence be in pair form with . Apply the comparators for (line L8). Then the subsequence is in pair form with amplitude at most .
Proof. The comparators have disjoint wires: the are different and the are different, and no wire is both a and an . So each comparator sees the items of the input of the round. The comparator on and exchanges them if and only if and , that is, if and only if and .
If , then gives , so no comparator exchanges, and the amplitude stays .
If , the exchanges happen exactly for . These values of satisfy , so all of them are in the round. After the round, for and for ; and for and for , because the items with became 1. Let and . Then because , and . So the subsequence is in pair form with amplitude .
Lemma 2 (steps 1 and 2). Suppose that the calls of lines L3 and L4 sort their subsequences. Then after line L4 the subsequence is in pair form with amplitude at most .
Proof. After line L2, for each , so the second items hold at least as many ones as the first items. Line L3 sorts the items , which are the subsequence of stride from , and line L4 sorts , the subsequence of stride from . These two calls have disjoint wires and do not change the number of ones of each row. Let be the number of zeros of the first row and that of the second row. Then , and since both rows are sorted, the subsequence is in pair form . Its amplitude is at most .
Theorem. For each , and for powers of 2 and , PAIRWISE-SORT sorts its subsequence for each 0-1 content and changes no other entry of . So by the zero-one principle, PAIRWISE-SORT sorts each array of items.
Proof. By induction on . If , line L1 returns. Let . The calls of lines L3 and L4 have items each, so by the induction hypothesis they sort their subsequences. By Lemma 2, after line L4 the subsequence is in pair form with amplitude .
If , then in line L6, the loop of line L7 does not run, and , so the subsequence is sorted.
If , the loop runs with . Before the round with the value , the amplitude is at most : for the first round, ; for each later round, Lemma 1 applied to the round before, with the value , gives an amplitude of at most . Each satisfies . So by Lemma 1, after the last round, with , the subsequence is in pair form with amplitude at most 1, and it is sorted. Each comparator of the call has both wires in the subsequence, so no other entry changes.