AlgoMaster Logo

Radix Sort

Low Priority6 min readUpdated July 10, 2026
Listen to this chapter
Unlock Audio

Radix Sort sorts numbers without ever comparing two of them directly. It processes one digit position at a time and uses a stable sorting algorithm like Counting Sort to reorder elements at each position. Repeating this across all digit positions produces a fully sorted array.

Loading simulation...

It runs in O(d * (n + k)) time, where d is the number of digits and k is the radix (10 for decimal). This makes it efficient for large sets of integers or fixed-length strings.

This chapter covers how Radix Sort works, how it builds on Counting Sort, and when it outperforms comparison-based sorting algorithms.

What Is Radix Sort?

Radix sort is a non-comparison sorting algorithm that processes elements one digit (or character) at a time. Instead of asking "is A greater than B?", it groups elements by the value of a specific digit position, then repeats for the next position until all positions have been processed.

The word "radix" means "base", as in the base of a number system. For decimal numbers, the radix is 10 (digits 0-9). For binary strings, the radix is 2. For lowercase English letters, the radix is 26.

Two Variants

There are two ways to process the digit positions:

  1. LSD (Least Significant Digit first): Start from the rightmost digit and move left. This is the more common variant and the one covered in this chapter. It naturally handles numbers with different digit counts and is easier to implement.
  2. MSD (Most Significant Digit first): Start from the leftmost digit and move right. This variant works well for strings and can short-circuit early, but it requires recursion and is more complex to implement.

Each pass uses a stable sort as a subroutine. Stability means that elements with the same digit value retain their relative order from the previous pass. Without stability, sorting by the tens digit would destroy the ordering produced by the ones-digit pass. Counting sort is commonly used as the subroutine because it is stable, runs in O(n + k) time, and works well for small ranges of values (like digits 0-9).

LSD vs MSD: A Quick Comparison

AspectLSDMSD
DirectionRight to left (ones, tens, hundreds, ...)Left to right (hundreds, tens, ones, ...)
ImplementationIterative, simplerRecursive, more complex
StabilityNaturally stableRequires care to maintain
Best forFixed-length integers, same-length stringsVariable-length strings, can short-circuit
Passes requiredAlways d passes (d = max digits)Can terminate early for some inputs

How It Works

The LSD radix sort algorithm follows these steps:

  1. Find the maximum value in the array to determine how many digit positions to process.
  2. For each digit position (starting from the least significant), use counting sort to sort the array based on the current digit. The counting sort only looks at one digit of each number, not the entire number.
  3. After processing all digit positions, the array is fully sorted.

Extracting a Digit

The digit at a given position is isolated with this formula:

Where position is 1 for the ones digit, 10 for the tens digit, 100 for the hundreds digit, and so on.

For example, to extract the tens digit of 753:

Why Does LSD Work?

Sorting by the least significant digit first produces a correct result because of stability. After sorting by the ones digit, all numbers with the same ones digit are grouped together. Sorting by the tens digit next groups numbers with the same tens digit, and within each group the relative order from the ones-digit pass is preserved. By the time the most significant digit is processed, each pass has refined the ordering, and stability ensures that earlier passes are never undone.

This is the same effect as sorting a spreadsheet by column C, then by column B, then by column A: the final result is sorted primarily by A, then by B within ties, then by C within further ties.

Code Implementation

Counting Sort as Subroutine

Radix sort needs a version of counting sort that sorts based on a specific digit position rather than the full value. The exp parameter indicates which digit position is being sorted (1 for ones, 10 for tens, 100 for hundreds).

Loading animation...

Complexity Analysis

MetricValueExplanation
TimeO(d * (n + k))d passes, each running counting sort in O(n + k)
SpaceO(n + k)Output array of size n, count array of size k
StableYesCounting sort subroutine preserves relative order
In-placeNoRequires O(n) extra space for the output array
Comparison-basedNoNever compares two elements directly

Where:

  • n = number of elements
  • d = number of digits in the maximum value
  • k = the radix (base), which is 10 for decimal numbers

When Is Radix Sort Faster Than O(n log n)?

Radix sort beats comparison-based sorts when d (n + k) < n log(n). Since k is typically small (10 for decimal), this simplifies to roughly d < log(n).

For 1 million 32-bit integers, log2(1,000,000) is about 20, and the maximum number of decimal digits is 10. So d = 10 < 20 = log(n), and radix sort wins. For sorting 10 numbers with 100 digits each, comparison sorts are faster.

Radix sort is well-suited to large datasets where the number of digits is small.

Stability

LSD radix sort is stable, and stability is required for the algorithm to produce a correct result. Each pass sorts by one digit while preserving the order established by previous passes. The earlier passes sorted by less significant digits, so within each group of equal current-digit values, the elements are already in the correct relative order; stability keeps them that way.

This is why the inner sort must be a stable algorithm. Counting sort is the standard choice. Swapping in an unstable inner sort (such as quicksort or heap sort) would break the relative order from previous passes, and the final array would not be sorted.

When to Use / When Not to Use

Good Use Cases

  • Fixed-length integers: Phone numbers, zip codes, social security numbers. The digit count is constant, which keeps d a small fixed value.
  • Large datasets with bounded values: Sorting millions of integers where the maximum value (and thus d) is moderate.
  • Same-length strings: Sorting fixed-length strings (like country codes or stock tickers) character by character.
  • When stability is required: Radix sort is inherently stable, which matters when sorting records by multiple keys.

Poor Use Cases

  • Variable-length strings: MSD radix sort can handle these, but the implementation is significantly more complex. For general-purpose string sorting, comparison-based sorts are often simpler and fast enough.
  • Floating-point numbers: The digit extraction formula does not work directly on floats. Floats must first be converted to a sortable integer representation.
  • When d is large relative to log(n): When numbers have many digits but the dataset is small, comparison sorts are faster.
  • Memory-constrained environments: Radix sort requires O(n) extra space. If memory is tight, an in-place sort like quicksort may be preferable.

Quiz

Radix Sort Quiz

10 quizzes