Radix sort

lo-fi, ambient, dreamy, relaxed

Listen on 93

Lyrics

[Verse 1]
Check the digits, non-comparative approach
Start from least significant, that's my coaching coach
Base ten breakdown, every number gets dissected
Counting sort foundation keeps the order protected
Bucket by position, zero through nine containers
Stable algorithm, original sequence maintainer
Linear time complexity when the range is bounded
O of n times k, efficiency compounded

[Chorus]
Radix sort, digit by digit we climb
Least to most significant, every single time
Stable sorting magic, preserve that relative order
Linear when the range don't cross no border
Count the frequencies, redistribute the load
Radix sort, cracking the sorting code

[Verse 2]
LSD first, that's least significant digit
MSD variant works but the logic gets rigid
Extract each position with division and modulo
Build the histogram, let the counting flow
Cumulative sums create the position mapping
Output array filling, no element trapping
Copy back the result, repeat for next digit
Perfect for integers when memory permits it

[Chorus]
Radix sort, digit by digit we climb
Least to most significant, every single time
Stable sorting magic, preserve that relative order
Linear when the range don't cross no border
Count the frequencies, redistribute the load
Radix sort, cracking the sorting code

[Bridge]
When comparison sorts hit n log n ceiling
Radix breaks through with a different feeling
Fixed-width keys unlock the linear dream
Counting sort engine powers the machine
String sorting possible with character codes
Distribution power, memory explodes

[Outro]
From least to most, we process every place
Stable transformation, numbers find their space
Radix revolution, sorting redefined
Linear time achieved with algorithmic mind

Story

# The Digital Warehouse Enigma ## 1. THE MYSTERY The morning chaos at DataFlow Logistics was unlike anything warehouse manager Sarah Chen had seen in her fifteen years. Their automated sorting system, responsible for processing millions of package IDs daily, had mysteriously begun producing perfect results in impossible time. Yesterday, sorting 10 million packages took the usual 47 minutes using their quicksort-based algorithm. But this morning, the same workload completed in just 12 minutes—with zero errors. "The logs show something impossible," muttered Jake, the lead systems engineer, staring at his terminal with wide eyes. "Our comparison counters are reading zero. Zero! How do you sort 10 million integers without a single comparison?" The sorting metrics dashboard displayed numbers that defied logic: linear time complexity across massive datasets, stable sorting maintained throughout, and memory usage that scaled perfectly with input size. Even more puzzling, the performance improvement was most dramatic when processing their largest package ID ranges—exactly the opposite of what traditional algorithms should exhibit. ## 2. THE EXPERT ARRIVES Dr. Elena Vasquez, the company's consulting algorithm specialist, arrived within the hour, her laptop bag slung over her shoulder and curiosity gleaming in her eyes. Known for her uncanny ability to diagnose algorithmic anomalies, she'd been called in when systems behaved in ways that seemed to violate computer science fundamentals. "Show me the data patterns," she said, settling at Jake's workstation. As she examined the logs, her eyebrows rose steadily higher. The system was processing package IDs—all 8-digit integers—in a way that suggested something far more sophisticated than traditional comparison-based sorting had taken over. ## 3. THE CONNECTION "I think I know what's happening here," Dr. Vasquez said, a knowing smile spreading across her face. "Sarah, what's the structure of your package IDs? Are they all the same length?" When Sarah confirmed they were consistently 8 digits, Elena nodded. "And your system has been updated recently?" Jake pulled up the deployment logs. "Actually, yes—we pushed a new sorting module last night. But it was supposed to be a minor efficiency patch." Elena examined the code commit and laughed. "This isn't quicksort anymore. Someone—accidentally or intentionally—implemented radix sort. Your system stopped comparing integers entirely and started examining them digit by digit." "But that's impossible," Sarah protested. "How do you sort without comparing?" Elena's eyes lit up with the joy of a teacher about to reveal a beautiful secret. "That's exactly what makes this so elegant. Your mysterious performance boost isn't magic—it's mathematics." ## 4. THE EXPLANATION "Think of it this way," Elena began, grabbing a whiteboard marker. "Traditional sorting algorithms are like having judges compare contestants in pairs—'Is A greater than B?' But radix sort is like organizing a filing cabinet. Instead of comparing entire package IDs, it looks at just one digit position at a time, starting from the rightmost digit." She drew ten boxes labeled 0 through 9. "For each digit position, we distribute all package IDs into these ten 'buckets' based on that single digit. No comparisons needed—just look at the digit and drop it in the right bucket. Then we collect them back in order and repeat for the next digit position." Jake leaned forward, fascinated. "But how does that maintain the overall order?" "That's where the stability property becomes crucial," Elena continued, warming to her subject. "When we process the ones place first, packages ending in 1 come before those ending in 2, and so on. When we move to the tens place, packages with the same tens digit maintain their relative order from the previous step. After eight passes—one for each digit position—everything is perfectly sorted." Elena pulled up the performance data. "Here's why your system suddenly became so fast. Traditional comparison sorts hit an O(n log n) wall—they simply can't do better than making roughly n log n comparisons. But radix sort breaks free from that limitation entirely. With fixed-length integers, it runs in O(d × (n + k)) time, where d is the number of digits, n is the number of elements, and k is the range of each digit—always 10 for decimal numbers. Since d is constant for your 8-digit IDs, you're getting true linear time performance." ## 5. THE SOLUTION "Let me show you exactly what happened with this morning's batch," Elena said, opening the system debugger. "Your 10 million package IDs got processed in 8 passes—one for each digit position. Each pass took linear time because the system just examined one digit and used counting sort as the underlying mechanism to distribute items into the ten buckets." Sarah watched as Elena traced through the algorithm's execution. "Pass 1 handled the ones place: all IDs ending in 0 went to bucket 0, those ending in 1 to bucket 1, and so forth. After collecting them back in order, pass 2 tackled the tens place, maintaining the stability from pass 1. By the eighth pass processing the leftmost digit, everything was perfectly ordered." "The beauty is in the mathematics," Elena continued. "Eight passes times 10 million operations each, plus the overhead of managing 10 buckets per pass. That's roughly 80 million simple operations instead of the 230 million comparisons your old quicksort required. No wonder the performance improved so dramatically." ## 6. THE RESOLUTION As Elena packed up her laptop, Sarah shook her head in amazement. "So our 'impossible' sorting speeds were actually just good algorithm design all along." The mysterious performance boost that had seemed to violate the laws of computer science turned out to be a textbook example of choosing the right tool for the job. "The real lesson," Elena said with a grin, "is that when your data has structure—like fixed-length integers—non-comparative algorithms can achieve what seems impossible. Your package IDs were perfect candidates for radix sort, and now you know why it's reigning supreme in your warehouse." As she headed for the door, Jake was already diving into the algorithm's implementation, eager to understand every detail of their accidental optimization.

← Bubble sort | Counting sort →