Algorithm Search⌘K

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 {0,1}n\{0, 1\}^n. In this proof all sequences are of zeros and ones, and positions are counted from 0.

Bitonic 0-1 sequences. Call x∈{0,1}mx \in \{0, 1\}^m bitonic when the set of positions tt with xt=1x_t = 1 is empty or one cyclic interval {a,a+1,…,a+ℓ−1}\{a, a + 1, \ldots, a + \ell - 1\} of Zm\mathbb{Z}_m, with the positions read modulo mm. The complement of a cyclic interval is empty or a cyclic interval, so xx is bitonic exactly when its zeros fill one cyclic interval or none. Each sorted sequence 0z1m−z0^{z} 1^{m - z} 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 x∈{0,1}2hx \in \{0, 1\}^{2h} be bitonic, and apply the comparators [t:t+h][t : t + h] for t=0,…,h−1t = 0, \ldots, h - 1 (line L10, with lo=0lo = 0). Let uu be the first half and vv the second half of the result. Then uu and vv are bitonic, uu or vv is clean, and us≤vtu_s \le v_t for all s,ts, t.

Proof. The comparators have disjoint wires, so ut=min⁡(xt,xt+h)u_t = \min(x_t, x_{t+h}) and vt=max⁡(xt,xt+h)v_t = \max(x_t, x_{t+h}). Let ℓ\ell be the number of ones of xx.

Case ℓ≤h\ell \le h. The ones fill a cyclic interval II of length ℓ≤h\ell \le h. The positions tt and t+ht + h have cyclic distance hh in both directions, so an interval that holds both has at least h+1h + 1 positions. So no tt has xt=xt+h=1x_t = x_{t+h} = 1, and uu is all zeros. Next, vt=1v_t = 1 if and only if t∈It \in I or t+h∈It + h \in I, that is, if and only if t≡i(modh)t \equiv i \pmod h for some i∈Ii \in I. The map i↦i mod hi \mapsto i \bmod h sends the ℓ≤h\ell \le h consecutive positions of II to ℓ\ell consecutive positions of Zh\mathbb{Z}_h, so the ones of vv fill one cyclic interval. So vv is bitonic, uu is clean, and us=0≤vtu_s = 0 \le v_t.

Case ℓ≥h\ell \ge h. Then xx has 2h−ℓ≤h2h - \ell \le h zeros, and they fill a cyclic interval. The argument above with zeros and ones exchanged shows that no tt has xt=xt+h=0x_t = x_{t+h} = 0, so vv is all ones, and the zeros of uu fill one cyclic interval. So uu is bitonic, vv is clean, and us≤1=vtu_s \le 1 = v_t. □\square

Lemma 2. For each power of 2 mm and each lolo, HALF-CLEANERS(A,lo,m)(A, lo, m) sorts A[lo…lo+m−1]A[lo \ldots lo + m - 1] if this part is a bitonic 0-1 sequence, and it changes no other entry of AA.

Proof. By induction on mm. If m=1m = 1, line L8 returns and one item is sorted. Let m=2h≥2m = 2h \ge 2. 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. □\square

Lemma 3 (the flip). Let A[lo…lo+m−1]A[lo \ldots lo + m - 1] with m=2hm = 2h 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 lo=0lo = 0. Let p=A[0…h−1]=0a1h−ap = A[0 \ldots h-1] = 0^{a} 1^{h-a} and q=A[h…m−1]=0b1h−bq = A[h \ldots m-1] = 0^{b} 1^{h-b}, and let qRq^R be qq in reverse order. The sequence y=p qR=0a12h−a−b0by = p \, q^R = 0^{a} 1^{2h - a - b} 0^{b} is bitonic, because its ones are consecutive. Line L5 compares AtA_t with Am−1−tA_{m-1-t}, and Am−1−t=qh−1−t=(qR)t=yt+hA_{m-1-t} = q_{h-1-t} = (q^R)_t = y_{t+h}. So the first half after line L5 is the first half uu of the half-cleaner applied to yy, and the second half after line L5 is the second half vv of that half-cleaner, in reverse order. By Lemma 1, uu and vv are bitonic and us≤vtu_s \le v_t for all s,ts, t. The reverse of vv is bitonic and holds the same items as vv. □\square

Theorem. For each power of 2 mm and each lolo, BITONIC-SORT(A,lo,m)(A, lo, m) sorts A[lo…lo+m−1]A[lo \ldots lo + m - 1] for each 0-1 content of this part, and it changes no other entry of AA. So, by the zero-one principle, BITONIC-SORT(A,0,n)(A, 0, n) sorts each array AA of nn items from a totally ordered set.

Proof. By induction on mm. If m=1m = 1, line L1 returns. Let m=2h≥2m = 2h \ge 2. 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 [lo,lo+m)[lo, lo + m). ■\blacksquare

Back to Bitonic sort