Algorithm Search⌘K

Proofs: Columnsort

These are the human proofs of Columnsort, at full length.

Columnsort sorts when r ≥ 2s² and s divides r full

This proof shows: correctness.

This proof shows the sufficient condition r≥2s2r \ge 2s^2 with s∣rs \mid r. Leighton's paper proves the sharper condition r≥2(s−1)2r \ge 2(s-1)^2 by a finer count of the mixed stretch in Lemma 3.

Index the matrix as M[i][k]M[i][k] with row i∈[0,r)i \in [0, r) and column k∈[0,s)k \in [0, s). The column-major index of M[i][k]M[i][k] is kr+ikr + i and its row-major index is is+kis + k.

Reduction to zeros and ones

A column sort is a sequence of compare-exchanges, and lines L2, L4, L6 and L8 move keys by fixed permutations, with fixed ±∞\pm\infty keys added on line L6 and removed on line L8. A compare-exchange commutes with every non-decreasing map ff on keys, and so does a fixed permutation, and ff can be extended to ±∞\pm\infty by f(±∞)=±∞f(\pm\infty) = \pm\infty. So columnsort commutes with ff. By the argument of the zero-one principle, if columnsort sorts every 0-1 matrix, it sorts every matrix. From now on the keys are zeros and ones, with −∞-\infty treated as 00 and +∞+\infty as 11.

Lemma 1. After line L2, column zero counts are monotone and close

Let zjz_j be the number of zeros in column jj after line L1, and let ZkZ_k be the number of zeros in column kk after line L2. Then Z0≥Z1≥⋯≥Zs−1≥Z0−sZ_0 \ge Z_1 \ge \cdots \ge Z_{s-1} \ge Z_0 - s.

Proof. After line L1, column jj is zjz_j zeros and then ones. Line L2 sends the key with column-major index tt to the cell with row-major index tt, that is, to column t mod st \bmod s. The keys of column jj have column-major indices jr,…,jr+r−1jr, \ldots, jr + r - 1, and since s∣rs \mid r, the key at offset uu in column jj goes to column u mod su \bmod s. Its zeros are at offsets u=0,…,zj−1u = 0, \ldots, z_j - 1, so column kk gets ⌊zj/s⌋+[k<zj mod s]\lfloor z_j / s \rfloor + [k < z_j \bmod s] zeros from column jj. Summing over jj,

Zk=∑j⌊zj/s⌋+#{ j:k<zj mod s }.Z_k = \sum_j \lfloor z_j / s \rfloor + \#\{\, j : k < z_j \bmod s \,\}.

The second term does not increase with kk, and it is between 00 and ss. □\square

Lemma 2. After line L3, at most s rows are mixed

Proof. After line L3, column kk has zeros in rows 0,…,Zk−10, \ldots, Z_k - 1 and ones below. By Lemma 1, row ii is all zeros when i<Zs−1i < Z_{s-1}, all ones when i≥Z0i \ge Z_0, and mixed only when Zs−1≤i<Z0Z_{s-1} \le i < Z_0. There are Z0−Zs−1≤sZ_0 - Z_{s-1} \le s such rows. □\square

Lemma 3. After line L5, the mixed stretch has at most s² keys and spans two columns

Proof. By Lemma 2, in row-major order after line L3, all keys before row-major index Zs−1sZ_{s-1} s are zeros and all keys from index Z0sZ_0 s on are ones. Call the indices between them the mixed stretch. It has at most s2s^2 indices. Line L4 sends the key with row-major index tt to the cell with column-major index tt, so after line L4, in column-major order, the matrix is zeros, then a stretch II of at most s2s^2 indices, then ones.

Since s2≤r/2<rs^2 \le r/2 < r, the stretch II meets at most two columns, say columns aa and a+1a + 1. Line L5 sorts each column. Columns before aa and after a+1a + 1 are clean, and clean columns stay clean. If II lies in one column, that column is now sorted, and the whole matrix is sorted in column-major order. Otherwise, column aa becomes zeros followed by ee ones, and column a+1a + 1 becomes bb zeros followed by ones, where ee and bb are at most the sizes of the parts of II in these columns. So e≤s2e \le s^2 and b≤s2b \le s^2, and in column-major order the matrix is

0⋯0  1⋯1⏟e  0⋯0⏟b  1⋯1,0 \cdots 0 \; \underbrace{1 \cdots 1}_{e} \; \underbrace{0 \cdots 0}_{b} \; 1 \cdots 1,

with the boundary between the two runs at column-major index (a+1)r(a+1) r. □\square

Lemma 4. Lines L6 to L8 sort the column-major order

Proof. In column-major order, the shifted matrix is ⌊r/2⌋\lfloor r/2 \rfloor keys −∞-\infty, then MM in column-major order, then ⌈r/2⌉\lceil r/2 \rceil keys +∞+\infty. So column k′k' of the shifted matrix holds the keys of MM with column-major indices in [k′r−⌊r/2⌋,(k′+1)r−⌊r/2⌋)[k' r - \lfloor r/2 \rfloor, (k'+1) r - \lfloor r/2 \rfloor), together with padding at the two ends. Line L7 sorts each such window, and line L8 puts the keys back at their column-major indices.

If MM is already sorted in column-major order, each window is sorted, and nothing changes. Otherwise MM has the form of Lemma 3. The window with k′=a+1k' = a + 1 is [(a+1)r−⌊r/2⌋,(a+2)r−⌊r/2⌋)[(a+1)r - \lfloor r/2 \rfloor, (a+2)r - \lfloor r/2 \rfloor). It contains the ee ones before index (a+1)r(a+1)r because e≤s2≤⌊r/2⌋e \le s^2 \le \lfloor r/2 \rfloor, and the bb zeros from index (a+1)r(a+1)r on because b≤s2≤⌈r/2⌉b \le s^2 \le \lceil r/2 \rceil. All keys of MM before this window are zeros and all keys after it are ones. Sorting the window makes the column-major order zeros followed by ones. The other windows are sorted already, as are the padding keys, since all −∞-\infty keys come first and all +∞+\infty keys last. □\square

Theorem

If s∣rs \mid r and r≥2s2r \ge 2s^2, columnsort sorts every r×sr \times s matrix into column-major order.

Proof. The condition gives s2≤⌊r/2⌋s^2 \le \lfloor r/2 \rfloor, which Lemmas 3 and 4 use. By Lemmas 1 to 4, columnsort sorts every 0-1 matrix, and by the reduction it sorts every matrix. □\square

Back to Columnsort