← Course home

Search and ordering · Lesson 23 of 52

Counting and radix sorts

Use bounded ranges and digit passes when key structure permits non-comparison sorting.

Full lessonAbout 10 min

You will learn to relate counting-sort cost to key range and explain stable digit passes and radix cost.

Recommended first: Comparison sorts.

On this page 6 sections

The idea

Keys 3, 1, 2, 1 with maximum 3 come out as 1, 1, 2, 3.

Comparison sorts need a logarithmic number of comparisons per item in the worst case when they learn order only by comparing keys. Counting sort skips comparisons when the keys are integers in a known range. counting_sort of 3, 1, 2, 1 with maximum 3 returns 1, 1, 2, 3. It counts how many keys have each value, then writes them into those slots. Equal keys keep their order if the write walks the input from left to right into the next free slot of that count. That stability is what a radix sort needs. lsd_radix_sort of 170, 45, 75, 90, 802, 24, 2, 66 returns 2, 24, 45, 66, 75, 90, 170, 802. Each pass sorts by one digit, starting at the least significant digit, and the next pass must not scramble the order the previous pass built. The digit alphabet is the base, here 10.

A worked trace

StepStateWhat changed
Placedigit = 1Place 2 copies of 1. Output is [1, 1].
Placedigit = 2Place 1 copies of 2. Output is [1, 1, 2].
Placedigit = 3Place 1 copies of 3. Output is [1, 1, 2, 3].

Why it is correct

The count array is a correct histogram because every key increments exactly one slot. The prefix of those counts says where each value should start in the output. Writing a key into its slot and advancing the slot places each key once. Stability holds when equal keys are written in the order they appear in the input. A radix pass is a counting sort on one digit. After the least significant digit is stable, sorting the next digit groups larger place values without reordering keys that share that digit. The highest digit pass finishes the order.

A common mistake

A huge maximum still allocates a count slot for every key up to that maximum.

What it costs

Counting sort is linear in the number of keys plus the size of the key range. A huge maximum with a tiny list still allocates the count array, so the range belongs in the bound. Radix sort repeats that cost once per digit. With a constant number of digits the total is linear in the number of keys. Extra memory is the count array and the output. These sorts are the right tool for integers or fixed-width strings, not for arbitrary ordered objects. Counting sort is Θ(n + k) time and uses Θ(k) extra memory.

Run it

python
from bitsandbytes.sorting.selection_and_radix import counting_sortprint(counting_sort([3, 1, 2, 1], maximum=3))# prints: [1, 1, 2, 3] 

Practice and next step

Open Order-statistic selection next. Order-statistic selection stops once the pivot lands on the rank you asked for.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.sorting.selection_and_radix

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Counting sort and digit passes

counting_sort of 3, 1, 2, 1 with maximum 3 returns 1, 1, 2, 3. A radix pass keeps equal digits in their old order.

digit
1

State

  1. 01chosen
  2. 11chosen

Place 2 copies of 1. Output is [1, 1].

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1counting_sort is told the maximum key. What does that bound pay for?
2A radix sort repeats a stable digit pass once per digit. What is the cost when the digit count is constant?

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.