Algorithm Search⌘K

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(A,lo,m,r)(A, lo, m, r) with L=m/r≥2L = m / r \ge 2 items in its subsequence, and let k=L/2k = L / 2 be the number of pairs. Write aia_i for the item on the wire lo+2irlo + 2ir and bib_i for the item on the wire lo+(2i+1)rlo + (2i + 1) r, for 0≤i<k0 \le i < k. The subsequence of the call is a0,b0,a1,b1,…,ak−1,bk−1a_0, b_0, a_1, b_1, \ldots, a_{k-1}, b_{k-1}. Each comparator of the call has both wires in this subsequence.

Definition. For integers 0≤β≤α≤k0 \le \beta \le \alpha \le k, the subsequence is in pair form (α,β)(\alpha, \beta) when ai=0a_i = 0 if and only if i<αi < \alpha, and bi=0b_i = 0 if and only if i<βi < \beta. Its amplitude is μ=α−β\mu = \alpha - \beta. In pair form the subsequence is 02β(0 1)μ12(k−α)0^{2\beta} (0\,1)^{\mu} 1^{2(k - \alpha)}. So it is sorted if and only if μ≤1\mu \le 1.

Lemma 1 (one round). Let g≥1g \ge 1 with g<kg < k, and let the subsequence be in pair form (α,β)(\alpha, \beta) with μ=α−β≤2g\mu = \alpha - \beta \le 2g. Apply the comparators [bi:ai+g][b_i : a_{i+g}] for 0≤i≤k−g−10 \le i \le k - g - 1 (line L8). Then the subsequence is in pair form with amplitude at most gg.

Proof. The comparators have disjoint wires: the bib_i are different and the ai+ga_{i+g} are different, and no wire is both a bb and an aa. So each comparator sees the items of the input of the round. The comparator on bib_i and ai+ga_{i+g} exchanges them if and only if bi=1b_i = 1 and ai+g=0a_{i+g} = 0, that is, if and only if i≥βi \ge \beta and i+g<αi + g < \alpha.

If μ≤g\mu \le g, then i≥βi \ge \beta gives i+g≥β+g≥αi + g \ge \beta + g \ge \alpha, so no comparator exchanges, and the amplitude stays μ≤g\mu \le g.

If g<μ≤2gg < \mu \le 2g, the exchanges happen exactly for β≤i<α−g\beta \le i < \alpha - g. These values of ii satisfy i≤k−g−1i \le k - g - 1, so all of them are in the round. After the round, bi=0b_i = 0 for i<α−gi < \alpha - g and bi=1b_i = 1 for i≥α−gi \ge \alpha - g; and aj=0a_j = 0 for j<β+gj < \beta + g and aj=1a_j = 1 for j≥β+gj \ge \beta + g, because the items aja_j with β+g≤j<α\beta + g \le j < \alpha became 1. Let α′=β+g\alpha' = \beta + g and β′=α−g\beta' = \alpha - g. Then β′≤α′\beta' \le \alpha' because μ≤2g\mu \le 2g, and α′≤α≤k\alpha' \le \alpha \le k. So the subsequence is in pair form (α′,β′)(\alpha', \beta') with amplitude α′−β′=2g−μ<g\alpha' - \beta' = 2g - \mu < g. □\square

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 kk.

Proof. After line L2, ai≤bia_i \le b_i for each ii, so the second items hold at least as many ones as the first items. Line L3 sorts the items a0,…,ak−1a_0, \ldots, a_{k-1}, which are the subsequence of stride 2r2r from lolo, and line L4 sorts b0,…,bk−1b_0, \ldots, b_{k-1}, the subsequence of stride 2r2r from lo+rlo + r. These two calls have disjoint wires and do not change the number of ones of each row. Let α\alpha be the number of zeros of the first row and β\beta that of the second row. Then β≤α\beta \le \alpha, and since both rows are sorted, the subsequence is in pair form (α,β)(\alpha, \beta). Its amplitude is at most α≤k\alpha \le k. □\square

Theorem. For each lolo, and for powers of 2 mm and rr, PAIRWISE-SORT(A,lo,m,r)(A, lo, m, r) sorts its subsequence for each 0-1 content and changes no other entry of AA. So by the zero-one principle, PAIRWISE-SORT(A,0,n,1)(A, 0, n, 1) sorts each array of n=2kn = 2^k items.

Proof. By induction on L=m/rL = m / r. If L≤1L \le 1, line L1 returns. Let L≥2L \ge 2. The calls of lines L3 and L4 have L/2L / 2 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 μ≤k\mu \le k.

If k=1k = 1, then g=0g = 0 in line L6, the loop of line L7 does not run, and μ≤1\mu \le 1, so the subsequence is sorted.

If k≥2k \ge 2, the loop runs with g=k/2,k/4,…,1g = k/2, k/4, \ldots, 1. Before the round with the value gg, the amplitude is at most 2g2g: for the first round, μ≤k=2g\mu \le k = 2g; for each later round, Lemma 1 applied to the round before, with the value 2g2g, gives an amplitude of at most 2g2g. Each gg satisfies 1≤g<k1 \le g < k. So by Lemma 1, after the last round, with g=1g = 1, 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. ■\blacksquare

Back to Pairwise sorting network