Level Up Your Coding Skills & Crack Interviews — Save up to 50% or more on Educative.io Today! Claim Discount

Arrow
Table of contents

Radix Sort Algorithm

Radix Sort sorts integers digit by digit, using a stable subroutine such as Counting Sort for each digit position.

What is Radix Sort?

Radix Sort is a non-comparison sorting algorithm that sorts integers by processing their digits one position at a time. Instead of directly comparing two numbers, it groups numbers according to the digit currently being examined.

The most common version is LSD Radix Sort, where LSD means least significant digit. It starts from the ones digit, then moves to the tens digit, hundreds digit, and so on until all digit positions have been processed.

Interview perspective: Radix Sort is important because it builds on Counting Sort and shows how sorting can sometimes beat comparison-based O(n log n) limits when inputs are integer-like and digit length is bounded.

Core Intuition

Radix Sort works by sorting numbers one digit at a time. First, it sorts all numbers by their ones digit. Then it sorts them by their tens digit. Then it sorts them by their hundreds digit, and so on.

The crucial detail is stability. When two numbers have the same current digit, their previous order must be preserved. This is what allows the earlier digit-level ordering to remain useful as we move to more significant digits.

What is tracked?

The digit at the current place value: ones, tens, hundreds, etc.

Why stability matters?

It preserves the order created by earlier digit passes.

Radix Sort Algorithm Steps

  1. Find the maximum value.
    The maximum value tells us how many digit positions must be processed.
  2. Start with the ones place.
    Use exponent exp = 1 to extract the ones digit.
  3. Stably sort by the current digit.
    Use Counting Sort or buckets so that equal digits keep their relative order.
  4. Move to the next digit.
    Multiply exp by 10 to move from ones to tens, then hundreds.
  5. Repeat until all digits are processed.
    Stop when max_num // exp == 0.
  6. Return the sorted array.
    After the most significant digit pass, the whole array is sorted.

Interactive Radix Sort Visualization

Use the visualization below to see how Radix Sort groups numbers into digit buckets for the ones, tens, and hundreds places. The buckets are collected in order after every pass.

Dry Run Example

Suppose we want to sort:

[170, 45, 75, 90, 802, 24, 2, 66]

Pass 1: Sort by ones digit

  • 170 has ones digit 0.
  • 90 has ones digit 0.
  • 802 and 2 have ones digit 2.
  • 24 has ones digit 4.
  • 45 and 75 have ones digit 5.
  • 66 has ones digit 6.

After collecting buckets from digit 0 to 9:

[170, 90, 802, 2, 24, 45, 75, 66]

Pass 2: Sort by tens digit

Now sort the current array by tens digit while preserving the order from the ones-digit pass.

[802, 2, 24, 45, 66, 170, 75, 90]

Pass 3: Sort by hundreds digit

Now sort by hundreds digit:

[2, 24, 45, 66, 75, 90, 170, 802]

The array is now sorted.

Radix Sort Code in Python

This implementation uses Counting Sort as a stable subroutine for each digit. It assumes non-negative integers.

Note: This version handles non-negative integers. To handle negative integers, you can separate negatives and positives, sort absolute values carefully, then combine them.

Time and Space Complexity

CaseComplexityWhy?
Time ComplexityO(d × (n + b))d is the number of digits, n is the number of elements, and b is the base, usually 10.
Best CaseO(d × (n + b))Radix Sort still processes every digit position.
Average CaseO(d × (n + b))Each digit pass uses stable Counting Sort.
Worst CaseO(d × (n + b))The number of passes depends on the maximum number of digits.
Space ComplexityO(n + b)Each digit pass uses an output array of size n and a count array of size b.

Why this can be fast: If the number of digits is small and fixed, Radix Sort behaves close to linear time.

When Should You Use Radix Sort?

Good for

  • Sorting non-negative integers
  • Fixed-length numeric keys
  • Large arrays with bounded digit length
  • Cases where comparison sorting is not required
  • Understanding how Counting Sort can be reused

Avoid for

  • Floating-point values
  • Mixed negative and positive numbers without adaptation
  • Very large digit lengths
  • Cases where stable intermediate sorting is unavailable
  • Small arrays where built-in sorting is simpler

Interview Notes and Common Pitfalls

This section highlights what interviewers usually expect you to understand about Radix Sort, especially its dependency on stable digit-level sorting.

What interviewers may expect you to know

  • Radix Sort is not comparison-based.
  • It sorts by digit positions rather than comparing whole numbers.
  • LSD Radix Sort processes digits from least significant to most significant.
  • Each digit pass must be stable.
  • Counting Sort is commonly used as a stable subroutine.
  • Its complexity depends on the number of digits and the base.

Common mistakes

  • Using an unstable sort for each digit pass.
  • Starting from the most significant digit, without changing the algorithm design.
  • Forgetting that basic Radix Sort assumes integer-like keys.
  • Ignoring negative numbers.
  • Confusing value range with digit length.
  • Claiming Radix Sort is always O(n) without explaining the digit factor.nd accidentally breaking stability.
  • Claiming the algorithm is always better than O(n log n) without considering k.

Related coding interview problems

  • Maximum Gap
  • Sort an Array
  • Relative Sort Array
  • Top K Frequent Elements
  • Sort Characters By Frequencys

Quick Quiz

Question 1: Why does Radix Sort need a stable sorting algorithm for each digit?

Because each digit pass must preserve the ordering created by earlier digit passes.

Question 2: What is the usual stable subroutine used in Radix Sort?

Counting Sort is commonly used because it can stably sort numbers by a single digit.

Question 3: What does d represent in O(d × (n + b))?it?

d represents the number of digit positions processed.

Key Takeaways

  • Radix Sort sorts numbers digit by digit.
  • LSD Radix Sort starts from the least significant digit.
  • Each digit pass must be stable.
  • Counting Sort is commonly used as the stable digit-level sort.
  • Its time complexity is O(d × (n + b)).
  • It works best for integer-like values with bounded digit length.

Share with others:

Unlock up to 68% off lifetime access to Coding Interview prep with Educative

Getting ready for coding interviews or sharpening your problem-solving skills? Unlock a lifetime discount with comprehensive resources designed to help you master technical interviews.

Data structures and algorithms

Pattern-based problem solving

Mock interview practice

Real-world coding challenges

Coding Interview Logo