Algorithm Search⌘K

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 [u:v][u : v], each of which puts the smaller of the keys at processors uu and vv on uu and the larger on vv. In a row sorted to the left, uu is the right one of the two neighbours. Such a step commutes with every non-decreasing map ff on keys: applying ff and then the step gives min⁡(f(a),f(b))=f(min⁡(a,b))\min(f(a), f(b)) = f(\min(a, b)) on uu and the same for the maximum on vv. By induction over the steps, shearsort commutes with ff. So the argument of the zero-one principle applies: if shearsort leaves some input unsorted in snake order, with keys a<ba < b at snake positions in the wrong order, then the 0-1 input f(x)=[x≥b]f(x) = [x \ge b] is also left unsorted. It is therefore enough to prove that shearsort sorts every r×cr \times c 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 0⋯01⋯10 \cdots 0 1 \cdots 1 and each odd row is 1⋯10⋯01 \cdots 1 0 \cdots 0.

Lemma 1. Two rows in opposite directions

Let row uu be sorted increasing to the right, with aa zeros, and let row vv be sorted increasing to the left, with bb 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 0,…,a−10, \ldots, a-1 and the zeros of the other are in the columns c−b,…,c−1c - b, \ldots, c - 1. If a+b≥ca + b \ge c, these two sets cover all cc columns, so each column has a zero and the row of minima is all zeros. If a+b<ca + b < c, each column has a one in at least one of the two rows, and the row of maxima is all ones. □\square

Lemma 2. Sorting a column depends only on its number of zeros

Proof. A sorted 0-1 column with zz zeros has zeros in its top zz cells and ones below. A compare-exchange inside the column does not change zz. □\square

Lemma 3. One phase halves the dirty rows

Suppose that before a phase, rows 0,…,A−10, \ldots, A - 1 are all zeros, rows r−B,…,r−1r - B, \ldots, r - 1 are all ones, and the D=r−A−BD = r - A - B rows between them are arbitrary. After lines L3 to L5 of the phase, the same holds with some A′≥AA' \ge A, B′≥BB' \ge B and D′=r−A′−B′≤⌈D/2⌉D' = r - A' - B' \le \lceil D/2 \rceil.

Proof. Line L3 keeps clean rows clean and sorts every row in its direction. Pair the middle rows as (A,A+1),(A+2,A+3),…(A, A+1), (A+2, A+3), \ldots, which gives ⌊D/2⌋\lfloor D/2 \rfloor 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 M′M'. In M′M' there are at least A+B+⌊D/2⌋A + B + \lfloor D/2 \rfloor clean rows, some all zeros and some all ones. Let ZZ be the number of rows of M′M' that are all zeros and OO the number that are all ones, so Z+O≥A+B+⌊D/2⌋Z + O \ge A + B + \lfloor D/2 \rfloor, Z≥AZ \ge A and O≥BO \ge B.

The thought experiment only exchanges keys inside columns, so by Lemma 2 sorting the columns of M′M' gives the same matrix as line L5 applied to the matrix after line L3. In M′M', each column has at least ZZ zeros and at least OO ones. So after the column sort, the top ZZ rows are all zeros and the bottom OO rows are all ones. This gives the claim with A′=ZA' = Z, B′=OB' = O and D′≤D−⌊D/2⌋=⌈D/2⌉D' \le D - \lfloor D/2 \rfloor = \lceil D/2 \rceil. □\square

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 A=B=0A = B = 0 and D=rD = r. By Lemma 3 and induction, after kk phases it holds with D≤⌈r/2k⌉D \le \lceil r / 2^k \rceil, so after ⌈lg⁡r⌉\lceil \lg r \rceil phases D≤1D \le 1. Line L6 sorts every row in its direction, keeps the top AA rows all zeros and the bottom BB 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. □\square

Step count

Line L3 and line L6 take cc steps each, and line L5 takes rr steps, by the bound for odd-even transposition sort. So the step count is ⌈lg⁡r⌉(r+c)+c\lceil \lg r \rceil (r + c) + c. For r=c=n=2kr = c = \sqrt{n} = 2^k, this is k⋅2k+1+2k=2k(2k+1)=n(lg⁡n+1)k \cdot 2^{k+1} + 2^k = 2^k (2k + 1) = \sqrt{n}(\lg n + 1).

Back to Shearsort