Algorithm Deep Dives

Radix Sort Explained

Radix Sort sorts integers digit by digit in O(nk) time using a stable sub-sort. Learn LSD vs MSD radix sort, how it works, and when it beats Quick Sort.

Aman Jaiman
6 min read
On this page +
  1. In plain English
  2. LSD vs MSD
  3. How LSD Radix Sort works
  4. When to use it
  5. Frequently asked questions

Radix Sort sorts numbers by processing their digits one place at a time, using a stable sort (usually Counting Sort) for each digit. By avoiding comparisons entirely, it can sort large arrays of fixed-width integers in O(nk) time, where k is the number of digits — often faster than O(n log n) comparison sorts.

LSD vs MSD #

There are two flavours. LSD (Least Significant Digit) radix sort starts from the rightmost digit and works left, relying on the stability of each pass to preserve earlier ordering — this is the common variant. MSD (Most Significant Digit) starts from the left and recurses, which suits variable-length strings.

How LSD Radix Sort works #

For each digit position from least to most significant, perform a stable Counting Sort using that digit as the key. After processing all digit positions, the array is fully sorted. The stability of each pass is essential — it ensures that the ordering established by less significant digits survives.

When to use it #

Radix Sort shines on large arrays of integers or fixed-length strings where k (digit count) is small relative to log n. For 32-bit integers, k is effectively constant, so it behaves like O(n). The cost is O(n + k) memory and the restriction to integer-like keys. See how it differs from Counting Sort in our comparison.

Found this useful?

Share it with someone who is learning this too.

Questions

Frequently asked questions

What is the time complexity of Radix Sort? +
O(nk), where n is the number of elements and k is the number of digits (or key length). For fixed-width integers k is constant, so it is effectively linear.
When should I use Radix Sort instead of Quick Sort? +
Use Radix Sort for large arrays of integers or fixed-length strings where the key length k is small relative to log n. It beats Quick Sort's O(n log n) in those cases.
Is Radix Sort stable? +
Yes, LSD Radix Sort is stable, which is required for it to work correctly — each digit pass must preserve the order established by previous passes.

See it in motion

Watch this algorithm and nine others run step by step, with live pseudocode and comparison counters.

Launch the visualiser
Aman Jaiman
Written by
Aman Jaiman

Software engineer at a stealth-stage startup, and previously a front-end engineer for around a year and a half.

Keep reading

Related guides

Link copied