Proofs: Shearsort
These are the human proofs of Shearsort, at full length.
Shearsort sorts into snake order full
This proof shows: correctness.
Reduction to zeros and ones
Each sort on lines L3, L5 and L6 is an odd-even transposition sort on one row or column. So shearsort is a fixed sequence of compare-exchanges , each of which puts the smaller of the keys at processors and on and the larger on . In a row sorted to the left, is the right one of the two neighbours. Such a step commutes with every non-decreasing map on keys: applying and then the step gives on and the same for the maximum on . By induction over the steps, shearsort commutes with . So the argument of the zero-one principle applies: if shearsort leaves some input unsorted in snake order, with keys at snake positions in the wrong order, then the 0-1 input is also left unsorted. It is therefore enough to prove that shearsort sorts every matrix of zeros and ones.
From now on the keys are zeros and ones. A row is clean if all its keys are equal and dirty otherwise. Odd-even transposition sorts each line, as shown on its page, so after line L3 each even row is and each odd row is .
Lemma 1. Two rows in opposite directions
Let row be sorted increasing to the right, with zeros, and let row be sorted increasing to the left, with zeros, or the other way round. Put the minimum of each column of the two rows in one row and the maximum in the other. Then the row of minima is all zeros or the row of maxima is all ones.
Proof. The zeros of one row are in the columns and the zeros of the other are in the columns . If , these two sets cover all columns, so each column has a zero and the row of minima is all zeros. If , each column has a one in at least one of the two rows, and the row of maxima is all ones.
Lemma 2. Sorting a column depends only on its number of zeros
Proof. A sorted 0-1 column with zeros has zeros in its top cells and ones below. A compare-exchange inside the column does not change .
Lemma 3. One phase halves the dirty rows
Suppose that before a phase, rows are all zeros, rows are all ones, and the rows between them are arbitrary. After lines L3 to L5 of the phase, the same holds with some , and .
Proof. Line L3 keeps clean rows clean and sorts every row in its direction. Pair the middle rows as , which gives pairs of neighbouring rows. Neighbouring rows have opposite directions. Apply, in a thought experiment, a compare-exchange to each column of each pair, minimum to the upper row. By Lemma 1, each pair now has a row of zeros or a row of ones. Call the result . In there are at least clean rows, some all zeros and some all ones. Let be the number of rows of that are all zeros and the number that are all ones, so , and .
The thought experiment only exchanges keys inside columns, so by Lemma 2 sorting the columns of gives the same matrix as line L5 applied to the matrix after line L3. In , each column has at least zeros and at least ones. So after the column sort, the top rows are all zeros and the bottom rows are all ones. This gives the claim with , and .
Theorem. Shearsort sorts every r × c matrix into snake order
Proof. By the reduction, take a 0-1 matrix. Before the first phase, the hypothesis of Lemma 3 holds with and . By Lemma 3 and induction, after phases it holds with , so after phases . Line L6 sorts every row in its direction, keeps the top rows all zeros and the bottom rows all ones, and sorts the one middle row, if there is one, in its direction. Read in snake order, the matrix is now a run of zeros followed by a run of ones, which is sorted.
Step count
Line L3 and line L6 take steps each, and line L5 takes steps, by the bound for odd-even transposition sort. So the step count is . For , this is .