AlgoMaster Logo

Radix Sort

Last Updated: March 31, 2026

Ashish

Ashish Pratap Singh

Radix Sort is a non-comparison-based sorting algorithm that processes elements digit by digit, starting from the least significant digit to the most significant (or vice versa). Instead of comparing values directly, it groups elements based on their digits at each position.

At each step, a stable sorting algorithm like Counting Sort is used to reorder the elements according to the current digit. By repeating this process across all digits, the array becomes fully sorted.

Radix Sort achieves linear time complexity O(n · d), where d is the number of digits, making it efficient for sorting large sets of integers or strings with fixed length.

In this chapter, you will learn how Radix Sort works step by step, how it builds on Counting Sort, and when it is a better choice than 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 we will focus on. 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.

The key insight is that 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. This is critical. Without stability, sorting by the tens digit would destroy the ordering we established when sorting by the ones digit. Counting sort is the standard choice for this subroutine because it is stable, runs in O(n + k) time, and works perfectly 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.
  1. After processing all digit positions, the array is fully sorted.

Extracting a Digit

To isolate the digit at a given position, we use 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?

This is the part that trips people up. How can sorting by the least significant digit first produce a correct result?

The answer is stability. When we sort by the ones digit, all numbers with the same ones digit are grouped together. When we then sort by the tens digit, numbers with the same tens digit are grouped, but within each group, the relative order from the ones-digit pass is preserved. By the time we reach the most significant digit, each pass has refined the ordering, and stability ensures that earlier passes are never undone.

Think of it like sorting a spreadsheet. If you sort 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. That is exactly what LSD radix sort does with digit positions.

Code Implementation

Counting Sort as Subroutine

Before implementing radix sort, we need 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 we are sorting by (1 for ones, 10 for tens, 100 for hundreds).

Example Walkthrough

Let us trace through the algorithm with the array [170, 45, 75, 90, 802, 24, 2, 66].

First, we find the maximum value: 802. It has 3 digits, so we need 3 passes.

Pass 1: Sort by Ones Digit (exp = 1)

We extract the ones digit of each number using (num / 1) % 10:

After counting sort by the ones digit:

Notice that 170 appears before 90 (both have ones digit 0) because 170 came first in the original array. Similarly, 802 appears before 2. Stability at work.

Pass 2: Sort by Tens Digit (exp = 10)

We extract the tens digit using (num / 10) % 10:

After counting sort by the tens digit:

802 and 2 both have tens digit 0. In the previous pass, 802 came before 2. Stability preserves this order. Similarly, 170 appears before 75 (both have tens digit 7).

Pass 3: Sort by Hundreds Digit (exp = 100)

We extract the hundreds digit using (num / 100) % 10:

After counting sort by the hundreds digit:

All the numbers with hundreds digit 0 retain their relative order from the previous pass: 2, 24, 45, 66, 75, 90. That order was correct because the previous two passes already sorted them by their tens and ones digits. The array is now fully sorted.

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)?

For radix sort to beat comparison-based sorts, we need 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 would be faster.

The takeaway: radix sort shines when the number of digits is small and the dataset is large.

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, making 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 you need stability: 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. You would need to convert to a sortable integer representation first.
  • When d is large relative to log(n): If your numbers have many digits but your dataset is small, comparison sorts will be faster.
  • Memory-constrained environments: Radix sort requires O(n) extra space. If memory is tight, an in-place sort like quicksort may be preferable.