TAOCP Vol 3: Sorting and Searching · Week 98

TAOCP 5.2.5 Sorting by Distribution

Updated

About 6 hours18 exercises, rated 13 to 40

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.

Flashcards 13 cards for Anki and AnkiMobile · all decks

Anki deck.tsv

This week's steps

3 steps
  1. Read §5.2.5 Sorting by Distribution

    Implement LSD radix sort on 32-bit integers with byte-sized digits and counting sort per pass.

  2. Work the exercises (7 picked)

    Start with Ex 2, Ex 1, Ex 8; log what you solve below.

  3. 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.

Sign in to track this: tick each step off and it counts toward your week.

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

  • Exercise 120▶ Recommended

    Would in-place distribution counting beat the linked radix sort? Compare the costs.

    Discuss

  • Exercise 213

    Is radix sort stable? It has to be, and the reason is the whole trick.

    Discuss

  • Exercise 423▶ Recommended

    Link the piles as stacks instead of queues and find an order that still sorts.

    Discuss

  • Exercise 6M24

    Expected number of empty piles after one distribution pass. The math stretch.

    Discuss

  • Exercise 820▶ Recommended

    Adapt radix sorting to two's complement and ones' complement keys.

    Discuss

  • Exercise 1422

    A card-dealing puzzle: sort a hand with two piles in two passes. Radix sort in disguise.

    Discuss

  • Exercise 1625▶ Recommended

    Sort strings of mixed lengths in time linear in their total length. The basis of practical string sorting.

    Discuss

Your log

Sign in to log the exercises you work: number, rating, how far you got.

Your notes

Only you

Sign in to keep private notes on this guide, in Markdown. They stay yours: never on your profile, and you can export them all.

Discussion

0 posts

Questions about the reading go here; each exercise has its own thread below. Hints over answers.

No posts yet. Ask a question or share a tip.

Sign in to postAnyone can read. Posting needs an account with a confirmed email.

Exercise threads

One thread per exercise. Open one to ask for a nudge or check your thinking.

  • Exercise 120No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 213No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 423No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 6M24No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 820No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 1422No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 1625No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

Reference

Why it matters

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
  • 5.2.4 Sorting by Merging
How to approach it

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

Common traps
  • Non-stable distribution passes break LSD radix sort.

Spot a mistake? Report it

Back to week 98