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 with . Leighton's paper proves the sharper condition by a finer count of the mixed stretch in Lemma 3.
Index the matrix as with row and column . The column-major index of is and its row-major index is .
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 keys added on line L6 and removed on line L8. A compare-exchange commutes with every non-decreasing map on keys, and so does a fixed permutation, and can be extended to by . So columnsort commutes with . 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 treated as and as .
Lemma 1. After line L2, column zero counts are monotone and close
Let be the number of zeros in column after line L1, and let be the number of zeros in column after line L2. Then .
Proof. After line L1, column is zeros and then ones. Line L2 sends the key with column-major index to the cell with row-major index , that is, to column . The keys of column have column-major indices , and since , the key at offset in column goes to column . Its zeros are at offsets , so column gets zeros from column . Summing over ,
The second term does not increase with , and it is between and .
Lemma 2. After line L3, at most s rows are mixed
Proof. After line L3, column has zeros in rows and ones below. By Lemma 1, row is all zeros when , all ones when , and mixed only when . There are such rows.
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 are zeros and all keys from index on are ones. Call the indices between them the mixed stretch. It has at most indices. Line L4 sends the key with row-major index to the cell with column-major index , so after line L4, in column-major order, the matrix is zeros, then a stretch of at most indices, then ones.
Since , the stretch meets at most two columns, say columns and . Line L5 sorts each column. Columns before and after are clean, and clean columns stay clean. If lies in one column, that column is now sorted, and the whole matrix is sorted in column-major order. Otherwise, column becomes zeros followed by ones, and column becomes zeros followed by ones, where and are at most the sizes of the parts of in these columns. So and , and in column-major order the matrix is
with the boundary between the two runs at column-major index .
Lemma 4. Lines L6 to L8 sort the column-major order
Proof. In column-major order, the shifted matrix is keys , then in column-major order, then keys . So column of the shifted matrix holds the keys of with column-major indices in , 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 is already sorted in column-major order, each window is sorted, and nothing changes. Otherwise has the form of Lemma 3. The window with is . It contains the ones before index because , and the zeros from index on because . All keys of 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 keys come first and all keys last.
Theorem
If and , columnsort sorts every matrix into column-major order.
Proof. The condition gives , 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.