Algorithm Search⌘K

Proofs: Radix sort (LSD)

These are the human proofs of Radix sort (LSD), at full length.

LSD radix sort sorts the items and is stable full

This proof shows: correctness, stability, termination.

Let b≥2b \ge 2 and let each key be an integer x≥0x \ge 0. Write xt=⌊x/bt⌋ mod bx_t = \lfloor x / b^t \rfloor \bmod b for the digit tt of xx, and x(t)=x mod btx^{(t)} = x \bmod b^{t} for the value of its lowest tt digits. Then x(t+1)=xtbt+x(t)x^{(t+1)} = x_t b^t + x^{(t)} with 0≤x(t)<bt0 \le x^{(t)} < b^t. We use one fact about this split.

Fact. For keys x,yx, y and t≥0t \ge 0: x(t+1)<y(t+1)x^{(t+1)} < y^{(t+1)} if and only if xt<ytx_t < y_t, or xt=ytx_t = y_t and x(t)<y(t)x^{(t)} < y^{(t)}.

Proof. If xt<ytx_t < y_t, then x(t+1)≤xtbt+bt−1<(xt+1)bt≤ytbt≤y(t+1)x^{(t+1)} \le x_t b^t + b^t - 1 < (x_t + 1) b^t \le y_t b^t \le y^{(t+1)}. If xt=ytx_t = y_t, the two values differ by x(t)−y(t)x^{(t)} - y^{(t)}. If xt>ytx_t > y_t, the first case with xx and yy swapped gives y(t+1)<x(t+1)y^{(t+1)} < x^{(t+1)}. □\square

Termination. Let mm be the largest key (0 for an empty array). The variable pp starts at 1 and is multiplied by b≥2b \ge 2 in each pass, so after t+1t + 1 passes p=bt+1≥2t+1p = b^{t+1} \ge 2^{t+1}. The test of line L6 is true as soon as bt+1>mb^{t+1} > m. So the loop runs for dd passes, where d=1d = 1 if m<bm < b and otherwise dd is the least integer with bd>mb^d > m. In each case bd>mb^d > m, and pass tt (for t=0,…,d−1t = 0, \ldots, d-1) runs with p=btp = b^t, so line L4 sorts by the digit xtx_t.

Claim. After pass tt, the array is sorted by key(t+1)\mathrm{key}^{(t+1)}, and items with equal key(t+1)\mathrm{key}^{(t+1)} are in their input order.

Proof by induction on tt. We say that the array before pass 0 is "after pass −1-1", and it is sorted by key(0)=0\mathrm{key}^{(0)} = 0 with all items in input order, so the claim holds for t=−1t = -1.

Assume the claim after pass t−1t - 1, for some t≥0t \ge 0. Pass tt is a stable sort by digit tt. Take two items uu and vv with keys xx and yy, where uu is in front of vv after pass tt. We show that x(t+1)≤y(t+1)x^{(t+1)} \le y^{(t+1)}, and that uu came first in the input if x(t+1)=y(t+1)x^{(t+1)} = y^{(t+1)}.

  1. If xt≠ytx_t \ne y_t: the sort by digit tt put uu first, so xt<ytx_t < y_t, and the Fact gives x(t+1)<y(t+1)x^{(t+1)} < y^{(t+1)}.
  2. If xt=ytx_t = y_t: the sort is stable, so uu was also in front of vv after pass t−1t - 1. By the induction hypothesis x(t)≤y(t)x^{(t)} \le y^{(t)}, and so x(t+1)≤y(t+1)x^{(t+1)} \le y^{(t+1)} by the Fact. If x(t+1)=y(t+1)x^{(t+1)} = y^{(t+1)}, then x(t)=y(t)x^{(t)} = y^{(t)}, and the induction hypothesis says that uu came first in the input.

Both cases give the claim after pass tt. □\square

Theorem. RADIX-SORT returns the items of AA in non-decreasing order of key, and items with equal keys are in their input order.

Proof. Each pass is a counting sort, which returns a permutation of its input, so the result holds the items of AA. After the last pass t=d−1t = d - 1, the Claim says that the array is sorted by key(d)\mathrm{key}^{(d)}, stably. Each key is at most m<bdm < b^d, so key(d)=key\mathrm{key}^{(d)} = \mathrm{key}. ■\blacksquare

Back to Radix sort (LSD)