Algorithm Search⌘K

Proofs: Radix exchange sort

These are the human proofs of Radix exchange sort, at full length.

Radix exchange sort sorts non-negative integers full

This proof shows: correctness, termination.

Keys are integers in [0,2w)[0, 2^w). For −1≤b<w-1 \le b < w, write top(x,b)=⌊x/2b+1⌋\mathrm{top}(x, b) = \lfloor x / 2^{b+1} \rfloor, the bits of xx above bit bb. A task (lo,hi,b)(lo, hi, b) is valid if all keys of A[lo…hi−1]A[lo \ldots hi - 1] have the same value of top(⋅,b)\mathrm{top}(\cdot, b).

Fact 1. If top(x,b)=top(y,b)\mathrm{top}(x, b) = \mathrm{top}(y, b) and bit(x,b)<bit(y,b)\mathrm{bit}(x, b) < \mathrm{bit}(y, b), then x<yx < y. If top(x,−1)=top(y,−1)\mathrm{top}(x, -1) = \mathrm{top}(y, -1), then x=yx = y.

Proof. Write t=top(x,b)t = \mathrm{top}(x, b). Then x=t⋅2b+1+0⋅2b+rx = t \cdot 2^{b+1} + 0 \cdot 2^b + r and y=t⋅2b+1+1⋅2b+r′y = t \cdot 2^{b+1} + 1 \cdot 2^b + r' with 0≤r,r′<2b0 \le r, r' < 2^b, so x<t⋅2b+1+2b≤yx < t \cdot 2^{b+1} + 2^b \le y. For b=−1b = -1, top(x,−1)=x\mathrm{top}(x, -1) = x. □\square

Lemma (partition). Let lines L5 to L9 run on a range [lo,hi)[lo, hi) with hi−lo≥2hi - lo \ge 2. They end, permute the range, and at the end every key of A[lo…i−1]A[lo \ldots i - 1] has bit bb equal to 0 and every key of A[i…hi−1]A[i \ldots hi - 1] has bit bb equal to 1.

Proof. At each test of line L6 the following holds: all keys of A[lo…i−1]A[lo \ldots i-1] have bit 0, all keys of A[j+1…hi−1]A[j+1 \ldots hi-1] have bit 1, and lo≤i≤j+1≤hilo \le i \le j + 1 \le hi. This is true at the start, with both parts empty. Line L7 moves ii only past keys with bit 0, and line L8 moves jj only past keys with bit 1, and both keep i≤j+1i \le j + 1, because they stop when i>ji > j. If line L9 swaps, then i<ji < j, and by the stops of L7 and L8, A[i]A[i] has bit 1 and A[j]A[j] has bit 0; after the swap A[i]A[i] has bit 0 and A[j]A[j] has bit 1, so both parts grow by one and i≤j+1i \le j + 1 still holds. Each turn of the loop of line L6 either moves ii or jj in L7 or L8, or swaps and moves both, because if neither L7 nor L8 moves, then i≤ji \le j and the conditions of both scans fail, which means A[i]A[i] has bit 1 and A[j]A[j] has bit 0, so i≠ji \ne j and line L9 runs. So j−ij - i decreases in each turn, and the loop ends with i=j+1i = j + 1. Then the two parts cover the range. □\square

Correctness. Call the state good if the tasks on SS have disjoint ranges and are valid, any two indices p<qp < q that are not in the same task have A[p]≤A[q]A[p] \le A[q], and AA holds the input keys. The first task (0,n,w−1)(0, n, w - 1) is valid, because top(x,w−1)=0\mathrm{top}(x, w - 1) = 0 for all keys, so the state is good at the start.

Pop a task (lo,hi,b)(lo, hi, b). If hi−lo≤1hi - lo \le 1, there is no pair inside it. If b<0b < 0, all its keys are equal by Fact 1, so all pairs inside it are in order. In both cases dropping the task keeps the state good. Otherwise the lemma splits the range into a part with bit b=0b = 0 and a part with bit b=1b = 1. Both parts agree on the bits above bb and on bit bb, so they are valid at b−1b - 1. A key of the first part and a key of the second part are in order by Fact 1. Pairs with one index outside the range keep their relation, because the range holds the same keys. So the state stays good after line L10.

Termination. Each task pushed by line L10 has a bit index one lower than the popped task, and tasks with b<0b < 0 push nothing. So each task has b≥−1b \ge -1, and the tasks form a binary tree of height at most w+1w + 1. The tree is finite, so the loop ends.

Result. When SS is empty, every pair p<qp < q has A[p]≤A[q]A[p] \le A[q], so AA is sorted, and it holds the input keys. ■\blacksquare

Back to Radix exchange sort