3.6.7 · D1Sorting & Searching

Foundations — Radix sort — LSD, MSD; O(d(n+k))

1,823 words8 min readBack to topic

Before you can trust the radix-sort machine in the parent note, you must own every piece of notation it fires at you. We build them in order, each one on top of the last. Nothing is used before it is drawn.


1. A key, and what "digit" really means

Look at the figure below. The number is drawn as three stacked columns. The picture is the whole point: a number is not atomic — it is addressable by position.

The value is rebuilt from its columns like this:


2. The symbol — the radix (base)

The picture below shows the tray rack for : ten labelled bins, digit on the left to digit on the right. Remember this rack — it is literally the count array in the parent's code.


3. The symbol — how many things we are sorting


4. The symbol — how many columns

How do we compute without drawing columns? We ask: how many times must I multiply by itself before I pass the biggest key? That question is answered by a logarithm.

The figure shows the staircase of powers of and where lands.


5. Reading one digit: the symbols //, %, and exp

The parent code plucks column out of a number with (x // exp) % k. Three symbols hide here; we build each.

Worked digit extraction on (base ):


6. The symbol — Big-O, "grows no faster than"

We build this properly in Big-O Notation. Here you only need the reading: means "do passes, each costing about work". The picture below contrasts (comparison sorts) against a flat line (radix when is a small constant).


7. The word stable — the hidden hero


8. LSD vs MSD — which column first

MSD's recursion-into-buckets is exactly the shape of a trie and cousins the divide-of-Bucket Sort; LSD is the flat, always--passes workhorse. The parent note compares them in full — here you only need the two names and that they differ in direction and structure.


Prerequisite map

Place value: a number is columns of digits

Radix k: k symbols, k trays

Extract digit: floor div and modulo

Counting Sort: one stable pass

Stability: ties keep order

Logarithm base k gives d

Big-O: d times n plus k

Radix Sort LSD and MSD


Equipment checklist

Test yourself — say the answer aloud, then reveal.

What does the radix count
The number of distinct symbols one digit can be, i.e. the number of trays/buckets.
What does count, versus
= items in the array; = possible digit values. They are unrelated.
Give the formula for
— the column count of the widest key.
What does ask
"To what power must I raise to reach ?" — it undoes exponentiation.
How do you extract column of key
— shift down by , then keep the ones column mod .
Why // exp before % k
Division aligns the wanted column into the ones place; modulo then keeps only that column.
What does read as in words
Do passes, each costing about work.
What does "stable" guarantee
Items tying on the current key keep their original relative order.
Why does radix sort need stability
So a later column's pass does not scramble the ordering earlier columns already fixed.
LSD vs MSD in one word each
LSD = rightmost-first flat loop; MSD = leftmost-first recurse-into-buckets.