Skip to content

Bytewise Radix Sort

Radix sort that processes fixed-width keys one byte at a time using 256 counting buckets.

Bytewise radix sort sorts fixed-width keys by processing one 8-bit byte at a time. Since each byte has 256 possible values, every pass uses a counting array of size 256.

It is a practical form of radix sort for machine integers, binary records, and fixed-size keys because byte extraction is cheap and bucket arrays fit easily in cache.

Problem

Given an array AA of nn fixed-width unsigned integers, sort the array in increasing order.

For a key with mm bytes, write it as:

x=dm1dm2d1d0 x = d_{m-1}d_{m-2}\cdots d_1d_0

where each byte satisfies:

0di<256 0 \le d_i < 256

Idea

Use stable counting sort on each byte position.

For LSD bytewise radix sort, process bytes from least significant to most significant:

d0,d1,,dm1 d_0, d_1, \ldots, d_{m-1}

Stability preserves the ordering established by earlier byte passes.

Algorithm

bytewise_radix_sort(A, bytes):
    for p from 0 to bytes - 1:
        stable_counting_sort_by_byte(A, p)

    return A

The byte at position pp is extracted by:

dp=(x>>(8p))&255 d_p = (x >> (8p)) \mathbin{\&} 255

Example

Sort 16-bit integers:

A=[513,256,258,1] A = [513, 256, 258, 1]

Byte representation:

valuehigh bytelow byte
51321
25610
25812
101

After sorting by low byte:

[256,513,1,258] [256, 513, 1, 258]

After sorting by high byte:

[1,256,258,513] [1, 256, 258, 513]

Correctness

After byte pass pp, the array is sorted by the suffix of bytes:

dpdp1d0 d_p d_{p-1}\cdots d_0

The base case holds after the first stable counting pass. For the induction step, sorting by byte dpd_p places keys in order by the new byte while preserving order by all lower bytes among equal byte values.

After all byte positions have been processed, the array is sorted by the complete key.

Complexity

Let:

  • nn be the number of keys
  • mm be the number of bytes per key

Each pass costs:

O(n+256) O(n + 256)

Total time:

O(m(n+256)) O(m(n + 256))

For fixed-width integers, mm is constant, so the time is linear in nn.

Space:

O(n+256) O(n + 256)

Properties

propertyvalue
stableyes
in placeno
comparison basedno
radix256
typical usefixed-width integer keys

When to Use

Use bytewise radix sort when sorting large arrays of fixed-width unsigned integers or binary keys. It is usually faster than comparison sorting when keys are cheap to inspect and the auxiliary output buffer is acceptable.

Avoid it for small arrays, variable-length strings, or signed values unless the signed ordering is handled explicitly.

Implementation

def bytewise_radix_sort(a, byte_count=None):
    if not a:
        return a

    if byte_count is None:
        byte_count = max(1, (max(a).bit_length() + 7) // 8)

    for p in range(byte_count):
        count = [0] * 256

        for x in a:
            b = (x >> (8 * p)) & 255
            count[b] += 1

        for i in range(1, 256):
            count[i] += count[i - 1]

        out = [0] * len(a)

        for i in range(len(a) - 1, -1, -1):
            x = a[i]
            b = (x >> (8 * p)) & 255
            count[b] -= 1
            out[count[b]] = x

        a = out

    return a