How does Radix Sort work?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Radix sort is a non-comparison sorting algorithm that orders values by processing their digits one position at a time. Instead of asking whether one element is less than another, it groups elements by digit buckets and relies on stable passes to build the final sorted order.
The Core Idea
Suppose you want to sort these integers:
In least-significant-digit radix sort, you first group numbers by the ones digit, then by the tens digit, then by the hundreds digit, and so on. The earlier passes still matter because each pass must be stable: elements with the same current digit keep the order established by previous passes.
That stability is what makes the whole algorithm work.
LSD Radix Sort Step by Step
For base 10 integers, each pass looks at one decimal digit.
- Sort by ones digit.
- Sort the result by tens digit.
- Sort the result by hundreds digit.
- Continue until the most significant digit of the largest number is processed.
After the ones-digit pass, the example becomes ordered by last digit. After the tens-digit pass, it is ordered by the last two digits. After the hundreds-digit pass, the whole list is sorted.
A Runnable Python Example
A common implementation uses counting sort as the stable subroutine for each digit:
This prints:
Why It Can Be Fast
If there are d digit positions and n items, radix sort typically runs in O(d * (n + b)), where b is the bucket count or radix. For fixed-size integers and a fixed radix, that is often close to linear in practice.
That is why radix sort can outperform comparison sorts on certain structured inputs, especially when keys are fixed-width integers or strings.
However, the algorithm is not universally superior. It needs extra memory, and its usefulness depends heavily on the shape of the data.
Stability Is Not Optional
The stable inner sort is the subtle but crucial part. If the per-digit sort were unstable, the ordering established by earlier digits would be destroyed, and the final result could be wrong.
So radix sort is really a strategy plus a requirement:
- strategy: sort one digit position at a time
- requirement: use a stable pass for each position
Common Pitfalls
One common mistake is forgetting that the inner digit sort must be stable. That breaks the algorithm even if each digit pass looks locally correct.
Another issue is assuming radix sort is always best. For general-purpose sorting of arbitrary objects, comparison sorts are often simpler and more flexible.
It is also easy to ignore special cases such as negative numbers, variable-length strings, or very large alphabets. A basic integer-only implementation does not automatically handle those well.
Summary
- Radix sort orders data by processing digits one position at a time.
- Least-significant-digit radix sort relies on stable sorting for each digit pass.
- A common implementation uses counting sort as the stable inner routine.
- Its time complexity is often
O(d * (n + b)), which can be very efficient for fixed-width keys. - The algorithm works best when the key structure is known and digit-based processing is practical.
Related reading
- How does Raft deals with delayed replies in AppendEntries RPC?
- How does Raft guarantee log consistency?
- How does raft preserve safty when a leader commits a log entry and crashes before informing followers this commitment?
- How does sorting a string in an array of strings and then sorting that array come out to be Oaslogalogs?
- How does the algorithm to color the song list in iTunes 11 work?
- How does the Hopcroft-Karp algorithm work?
- How does stdsort work for list of pairs?
- How does structural recursion differ from generative recursion?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.