Distribution sorting algorithms

Bucket sort

(also known as Address Calculation Sort)

Note: this differs from CLR.

Assumes that, for all i, Ki in {1...m}.

BUCKET SORT
    for j := 1 to m do
       Lj := Λ
    for i := 1 to n do
       append Ki to end of LKi
    L := Λ
    for j := 1 to m do
       concatenate Lj to end of L

T is Θ(m + n)
S is Θ(m) for listheads

Lexicographic (Radix) sort

(see CLR pp.197-199)

Do a bucket sort on the least significant digit of the keys, keeping those with equal digits in the same relative order.
Iterate on the next significant digit, etc.

LEXICOGRAPHIC SORT
    Q := Λ
    for j := 0 to M-1 do
       Qj := Λ
    Q ← K1, ..., Km
    for t := r downto 1 do
       while Q ≠ Λ do
          Ki ← Q
          Qdi,t ← Ki
       for j := 0 to M-1 do
          Q := Q | Qj

T is Θ((M+n)r)
S is Θ(M+n)

Example:

Numbers in radix-3 (ternary): 3, 14, 1, 5, 9, 2, 6

Initial after pass 1 after pass 2 after pass 3
 010
 112
 001
 012
 100
 002
 020
   010
   100
   020
   001
   112
   012
   002
   100
   001
   002
   010
   112
   012
   020
   001
   002
   010
   012
   020
   100
   112


Dan Hirschberg
Last modified: Oct 28, 2003