Skip to the workspace
Pebellslearn by building
Sorting at Scale  ›  Lesson

Counting, Bucket & Radix Sort

Non-Comparison Sorts 26 minSorting without comparisons: count occurrences, turn counts into positions, and place each item directly

dsa / Counting, Bucket & Radix Sort

Explore each scene at your own pace.

01 · Sort record IDs without comparing them

Sort record IDs without comparing them

Counting, Bucket & Radix Sort

Definition
Counting sort orders whole numbers without comparing them: it counts how often each value occurs, turns the counts into final positions, and places each item directly. Radix sort applies a stable counting sort to one digit at a time to handle larger numbers.
In plain words
Think of sorting exam papers into labelled pigeonholes by score, then reading the holes in order. Nobody asks which paper is better than which; each paper just goes to its slot. For big numbers, you do this once per digit, starting from the last.

In short: every sort in section 3 decided order by comparing two items. Counting sort never compares anything — it counts how many times each value occurs, turns those counts into positions with a running total, then places each item straight into its final slot. That makes it O(n + k), where k is the range of values, beating any comparison sort's O(n log n) floor. But it only works on a small range of non-negative integers. Radix sort lifts that limit by running a stable counting sort once per digit, from the ones place up, so it can sort numbers of any size in O(d·n), where d is the digit count. Both are stable — and they have to be, or radix sort's later digit-passes would undo the sorting the earlier passes did.

Three stacked rows — the input array, a counts row, and an output row — with one curved arrow looping from the last input box back to an early output slot, showing the backward scan that keeps stability

Sort record IDs without comparing them

Quarry ingests a batch of small non-negative record IDs overnight and needs them sorted before the morning read. Counting sort never asks "is this one bigger than that one" — it only counts:

python

The prefix-summed counts array turns "how many 2s are there" into "how many items belong at or before a 2" — exactly the final index each item needs, counting from the back so that the last equal item lands in the highest open slot, leaving earlier arrivals the lower ones. Watch the output array fill in, one placement at a time:

watch it move · array

Scanning the input backward and placing straight into the final slot is what keeps equal values in their original order.

The last input value is 2 (index 5). Its running count says "one belongs at position 3". 6 cells. At index 3.

1 / 6
Scene 1 of 7
Project context and further resources
You're building a piece ofQuarry — the ingest sorter
This piece — radix_sort(): Sorts a batch of non-negative record IDs without ever comparing two of them.
Scenario A batch of record IDs lands overnight and needs sorting before the morning read, without ever comparing two IDs directly.
radix_sort.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
radix_sort(nums) → listA new list with nums sorted ascending, using repeated stable counting-sort passes, one digit at a time.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.