Menu
Coddy logo textTech

Radix Sort

Last updated

Radix sort is a non-comparison sort for integers. Instead of comparing values, it sorts numbers digit by digit. The least-significant-digit (LSD) version processes the ones digit first, then the tens, then the hundreds, using a stable counting sort at each digit. Because each pass is stable, once the most significant digit is processed the whole array is sorted. Press play above to watch each digit pass reorder the bars.

Radix sort runs in O(d·(n + k)) time, where d is the number of digits and k is the base (10 here). For fixed-width integers this is effectively linear - it can beat O(n log n) comparison sorts - but it only works on data that can be broken into digits or keys.

Time & space complexity

CaseComplexityNotes
TimeO(d·(n + k))d digits, base k (linear for fixed d)
SpaceO(n + k)Output array + digit counts
StableYesEach digit pass is a stable counting sort
Comparison?NoSorts by digit, not by comparing values
Works onIntegers/keysNot general comparable objects

Step by step

StepWhat happens
1Find the maximum value to know how many digits to process.
2Start with the least-significant digit (the ones place).
3Stably sort the array by that digit using counting sort.
4Move to the next more-significant digit.
5Repeat until all digit positions are processed.

Worked example

Sorting [170, 45, 75, 90, 2, 24, 66]:

PassArrayAction
Start[170, 45, 75, 90, 2, 24, 66]Max is 170, so three digit passes are needed.
Ones[170, 90, 2, 24, 45, 75, 66]Stable sort by the ones digit: 0, 0, 2, 4, 5, 5, 6.
Tens[2, 24, 45, 66, 170, 75, 90]Stable sort by the tens digit: 0, 2, 4, 6, 7, 7, 9 (170 keeps its lead over 75).
Hundreds[2, 24, 45, 66, 75, 90, 170]Stable sort by the hundreds digit; only 170 has a 1, so it moves last. Sorted.

When to use radix sort

Use it whenAvoid it when
Keys are integers or fixed-length strings you can split into digits.You must sort arbitrary objects by a custom comparator.
Keys have a small, bounded number of digits d, so O(d·(n + k)) beats O(n log n).Keys are very long or unbounded, making d large and the passes expensive.
You need a stable sort and can afford O(n + k) extra space.Memory is tight and the O(n + k) buffers are unacceptable.
The value range or base k is modest relative to n.k is huge, so each counting-sort pass dominates the running time.

Radix Sort code

A clean, runnable Radix Sort implementation in Python, JavaScript, Java, C++, C. Pick a language, copy the code, or open it pre-loaded in the Coddy Playground.

Radix Sort code in Python

Python
1def radix_sort(a):2    # Sort by each decimal digit, least significant first3    max_value = max(a)4    exp = 15    while max_value // exp > 0:6        a = sort_by_digit(a, exp)7        exp *= 108    return a9
10
11def sort_by_digit(a, exp):12    buckets = [[] for _ in range(10)]13    for value in a:14        digit = (value // exp) % 1015        buckets[digit].append(value)16    # Concatenating buckets 0..9 keeps the sort stable17    return [value for bucket in buckets for value in bucket]18
19
20nums = [170, 45, 75, 90, 802, 24, 2, 66]21print("Before:", nums)22print("After: ", radix_sort(nums))
Run this code in the Python Playground

Radix Sort FAQ

What is the time complexity of radix sort?
Radix sort is O(d·(n + k)), where d is the number of digits and k is the base. For integers of fixed width this is effectively O(n), which can be faster than comparison sorts. It uses O(n + k) extra space.
Is radix sort stable?
Yes. LSD radix sort relies on a stable counting sort at each digit; stability is what makes the digit-by-digit approach produce a correctly sorted result.
When can I use radix sort?
Radix sort works on data that can be decomposed into digits or fixed-size keys, such as integers or fixed-length strings. It is not a general-purpose comparison sort, so it cannot sort arbitrary objects by a custom comparator.
How is radix sort different from counting sort?
Counting sort sorts by a single key in one pass and needs a count array as large as the value range, so it degrades when values are spread out. Radix sort applies counting sort digit by digit, keeping each pass's count array small (base k), which lets it handle large value ranges that plain counting sort could not.
Why does LSD radix sort start with the least-significant digit?
Starting from the least-significant digit lets each stable pass preserve the ordering established by all earlier, less-significant digits. By the time the most-significant digit is processed, ties on that digit are already correctly ordered by the lower digits, so the array ends up fully sorted. Sorting from the most-significant digit first would break this and require a different, recursive approach (MSD radix sort).
Can radix sort handle negative numbers?
Not directly - the basic digit extraction assumes non-negative integers. Common fixes are to offset all values by adding the minimum so everything is non-negative, or to sort the negatives and non-negatives separately and then concatenate. Ignoring this is a frequent bug when applying radix sort to real data.
Coddy programming languages illustration

Master algorithms with Coddy

GET STARTED