Algorithm Search⌘K

Proofs: Parallel radix sort

These are the human proofs of Parallel radix sort, at full length.

Parallel radix sort is correct and stable full

This proof shows: correctness.

Fix a pass tt and write d(x)=digit⁡t(x)d(x) = \operatorname{digit}_t(x). Give the items of AA their input positions 0,…,n−10, \ldots, n-1. Block jj holds the positions ⌊jn/p⌋\lfloor jn/p \rfloor to ⌊(j+1)n/p⌋−1\lfloor (j+1)n/p \rfloor - 1, so the blocks cut the positions into consecutive ranges in increasing order of jj.

Lemma 1. One pass is a stable counting sort on digit t

Define the order by digit tt: item uu comes before item vv if d(u)<d(v)d(u) < d(v), or d(u)=d(v)d(u) = d(v) and uu has the smaller position. After the pass, the item with qq items before it in this order is at position qq of BB.

Proof. Let xx be an item in block jj with digit dd. The items before xx in the order by digit tt are of three kinds:

  1. the items with a digit less than dd, in any block; there are ∑d′<d∑j′C[d′][j′]\sum_{d' < d} \sum_{j'} C[d'][j'] of them;
  2. the items with digit dd in a block j′<jj' < j, since these have smaller positions; there are ∑j′<jC[d][j′]\sum_{j' < j} C[d][j'] of them;
  3. the items with digit dd in block jj that are to the left of xx.

By the order of the prefix sum on line L4, the starting value of O[d][j]O[d][j] is the sum of the counts of the first two kinds. Processor jj visits the keys of its block from left to right, and adds 1 to O[d][j]O[d][j] after each key with digit dd, so when it reaches xx the counter has grown by the number of items of the third kind. So xx is written at position qq, where qq is the number of items before xx.

The map from items to qq is a bijection onto 0,…,n−10, \ldots, n-1, because the order by digit tt is a strict total order on the nn items. So every cell of BB is written exactly once. □\square

Lemma 2. Exclusive access

Proof. On line L3, processor jj reads only its own block and writes only the counts C[⋅][j]C[\cdot][j]. The prefix sum on line L4 can be done with exclusive reads and writes, as shown for the EREW PRAM on the page on prefix sums. On lines L5 to L7, processor jj reads its block and its own counters O[⋅][j]O[\cdot][j], and by Lemma 1 the cells of BB it writes are distinct from each other and from those of every other processor. So no cell is read or written by two processors in the same step. □\square

Theorem. PAR-RADIX-SORT sorts stably

After the passes 0,…,t0, \ldots, t, the items are in the order of their low t+1t + 1 digits, read as a number, with ties in input order. So after all ⌈w/b⌉\lceil w/b \rceil passes they are sorted, stably.

Proof. By induction on tt. For t=0t = 0 this is Lemma 1 with the input order. Assume it after pass t−1t - 1, and take two items uu and vv with uu before vv after pass tt. By Lemma 1, either dt(u)<dt(v)d_t(u) < d_t(v), and then the low t+1t + 1 digits of uu form a smaller number, or dt(u)=dt(v)d_t(u) = d_t(v) and uu was before vv after pass t−1t - 1. In the second case, by induction, the low tt digits of uu form a number at most that of vv, with uu earlier in the input if they are equal. In both cases uu comes before vv in the order of the claim. A key of ww bits has all its bits in the low ⌈w/b⌉\lceil w/b \rceil digits, so the last pass leaves the keys sorted, with equal keys in input order. □\square

Back to Parallel radix sort