# TAOCP 5.2.5 Sorting by Distribution

> Study guide to TAOCP 5.2.5 Sorting by Distribution. Radix sorting: distributing records into piles by digit, least significant digit first, using linked…

Updated October 20, 2026.

## Summary {#summary}

TAOCP 5.2.5 Sorting by Distribution: Radix sorting: distributing records into piles by digit, least significant digit first, using linked lists to avoid moving data, with analysis of when it beats comparison sorts. Budget about 6 hours; do exercises 2, 1 and 8.

## Key facts {#key-facts}

- Week 98 of 145 on the cracked.guide path, in [TAOCP Vol 3: Sorting and Searching](https://cracked.guide/taocp/volume-3).
- Time budget: about 6 hours.
- 18 exercises in the section, rated 13 to 40; the guide picks 7.
- Picked exercises with Knuth's triangle for recommended: 1, 8, 4 and 16.
- Before you start: 5.2.4 Sorting by Merging.

## Week 98 goals {#objectives}

By the end of week 98 you can:

- Trace a least-significant-digit radix sort pass on decimal keys.
- Implement LSD radix sort with counting sort on byte-sized digits.
- Explain when radix sorting beats comparison sorting.

## This week's steps {#steps}

- Read §5.2.5 Sorting by Distribution: Implement LSD radix sort on 32-bit integers with byte-sized digits and counting sort per pass.
- Work the exercises (7 picked): Start with Ex 2, Ex 1, Ex 8; log what you solve below.
- Write down one thing you'd explain to someone: A few sentences in your own words in Your notes: the idea, why it works, an example. 30 words ticks this.

## Exercises {#exercises}

This section has 18 exercises, rated 13 to 40, and Knuth marks 4 of them with the triangle for recommended. Implement LSD radix sort with linked queues as in Algorithm R, then do 8 so it handles negative numbers. 16 is the practical one: sorting variable-length strings in linear time. 13 is a research-sized implementation project.

Worth your time:

- 2 (rated 13): Is radix sort stable? It has to be, and the reason is the whole trick.
- 1 (rated 20, recommended): Would in-place distribution counting beat the linked radix sort? Compare the costs.
- 8 (rated 20, recommended): Adapt radix sorting to two's complement and ones' complement keys.
- 14 (rated 22): A card-dealing puzzle: sort a hand with two piles in two passes. Radix sort in disguise.
- 4 (rated 23, recommended): Link the piles as stacks instead of queues and find an order that still sorts.
- 16 (rated 25, recommended): Sort strings of mixed lengths in time linear in their total length. The basis of practical string sorting.
- 6 (rated M24): Expected number of empty piles after one distribution pass. The math stretch.

## Why it matters {#why}

Radix sort is how you sort integers, strings and fixed-size keys fast. It shows up in GPU sorting and database engines.

## Before you start {#before}

- 5.2.4 Sorting by Merging

## How to approach it {#approach}

Implement LSD radix sort on 32-bit integers with byte-sized digits and counting sort per pass. Benchmark it against your quicksort.

## Common traps {#traps}

- Non-stable distribution passes break LSD radix sort.

## Links {#links}

- [Knuth's TAOCP page (editions, errata, MIX/MMIX)](https://www-cs-faculty.stanford.edu/~knuth/taocp.html)
- [Vol 3 errata (plain text, from Knuth)](https://www-cs-faculty.stanford.edu/~knuth/err3.textxt)
- [Wikipedia: Radix sort](https://en.wikipedia.org/wiki/Radix_sort)
- [Wikipedia: Counting sort](https://en.wikipedia.org/wiki/Counting_sort)

---

Source: https://cracked.guide/guide/knuth-w081-0
cracked.guide: A free, dated 145-week path to getting genuinely cracked at programming.
