Skip to content

11.10   Radix sort

The previous section introduced counting sort, which works well when the number of elements \(n\) is large but the range of values \(m\) is small. Suppose we need to sort \(n = 10^6\) student IDs, each \(8\) digits long. The range of values, \(m = 10^8\), is so large that counting sort would require a great deal of memory. Radix sort avoids this problem.

Radix sort shares counting sort's basic idea of using counts to order elements. It also takes advantage of place value: sorting by one digit at a time, in succession, produces the final sorted order.

11.10.1   How the algorithm works

For the student IDs, number the digits from \(1\) at the least significant end to \(8\) at the most significant end. Figure 11-18 shows how radix sort proceeds.

  1. Initialize the digit position \(k = 1\).
  2. Apply counting sort to digit \(k\) of the student IDs. The IDs are now in ascending order by digit \(k\).
  3. Increase \(k\) by \(1\) and return to step 2.. Stop after sorting by every digit.

Figure 11-18   Radix sort procedure

Description of Figure 11-18: Stable radix sort from the ones place to the most significant digit

Ten eight-digit integers are stably sorted one digit at a time, from digit 1 (ones) through digit 8 (ten millions), ending in ascending order by full numeric value.

The input array nums contains ten eight-digit decimal integers, such as student IDs (the table gives their full values). Digit positions k start at 1. Digit 1, on the far right, is the least significant (ones), digit 2 is the tens digit, and so on up to digit 8, the most significant, on the far left (ten millions).

The four data columns show the input, the order after sorting by ones, the order after sorting by tens, and the final result. Read each column independently from top to bottom for array indices 0 through 9. A row shows the same current position in each state, not the path of one integer across passes. The five passes for digits 3 through 7 all run, although their intermediate states are omitted.

Each pass uses stable counting sort on the current digit. Two numbers with the same digit keep their relative order from the previous pass. For example, 35663510 and 88906420 both end in 0 and keep their original order after pass 1. The tens digits of 81883077 and 30524779 are both 7, so pass 2 retains their order from pass 1: 81883077, ending in 7, precedes 30524779, ending in 9.

More significant digits have higher priority when comparing numbers, so each later pass takes precedence over earlier ones. Where the higher digits are equal, stability preserves the order established by lower digits. Each pass moves entire eight-digit integers, never just the individual digits in one place.

Array at each stage: read each column separately
Current position (index)Input orderAfter sorting by onesAfter sorting by tensFinal result
010546151356635103566351010546151
135663510889064208890642030524779
242865989105461518206033734862445
334862445724292447242924435663510
481883077348624453486244542865989
588906420638329961054615163832996
672429244818830778188307772429244
730524779820603373052477981883077
882060337428659894286598982060337
963832996305247796383299688906420

Starting with the least significant digit and preserving stability on every pass together ensure that the full values end up in order.

Let's look at the implementation. For a base-\(d\) number \(x\), we can obtain digit \(k\), denoted by \(x_k\), with the following formula:

\[ x_k = \lfloor\frac{x}{d^{k-1}}\rfloor \bmod d \]

Here \(\lfloor a \rfloor\) rounds the floating-point number \(a\) down to an integer, and \(\bmod \: d\) takes the remainder after division by \(d\). For the student IDs, \(d = 10\) and \(k \in [1, 8]\).

We also need to adapt counting sort slightly so that it sorts numbers by digit \(k\):

radix_sort.py
def digit(num: int, exp: int) -> int:
    """Get digit k of num, where exp = 10^(k-1)"""
    # Passing exp instead of k avoids repeating the costly exponentiation here
    return (num // exp) % 10

def counting_sort_digit(nums: list[int], exp: int):
    """Counting sort by digit k of each element in nums"""
    # Decimal digits range from 0 to 9, so the bucket array needs 10 entries
    counter = [0] * 10
    n = len(nums)
    # Count the occurrences of digits 0 through 9
    for i in range(n):
        d = digit(nums[i], exp)  # Let d be digit k of nums[i]
        counter[d] += 1  # Count this occurrence of digit d
    # Compute prefix sums to convert counts into array positions
    for i in range(1, 10):
        counter[i] += counter[i - 1]
    # Traverse backward and use the bucket counts to place each element in res
    res = [0] * n
    for i in range(n - 1, -1, -1):
        d = digit(nums[i], exp)
        j = counter[d] - 1  # Get the array index j for d
        res[j] = nums[i]  # Place the current element at index j
        counter[d] -= 1  # Decrement the count for d by 1
    # Copy the result back into nums
    for i in range(n):
        nums[i] = res[i]

def radix_sort(nums: list[int]):
    """Radix sort"""
    # Find the largest element to determine the maximum number of digits
    m = max(nums)
    # Work from the least significant digit to the most significant
    exp = 1
    while exp <= m:
        # Apply counting sort to the array elements by digit k
        # k = 1 -> exp = 1
        # k = 2 -> exp = 10
        # Here exp = 10^(k-1)
        counting_sort_digit(nums, exp)
        exp *= 10
radix_sort.cpp
/* Get digit k of num, where exp = 10^(k-1) */
int digit(int num, int exp) {
    // Passing exp instead of k avoids repeating the costly exponentiation here
    return (num / exp) % 10;
}

/* Counting sort by digit k of each element in nums */
void countingSortDigit(vector<int> &nums, int exp) {
    // Decimal digits range from 0 to 9, so the bucket array needs 10 entries
    vector<int> counter(10, 0);
    int n = nums.size();
    // Count the occurrences of digits 0 through 9
    for (int i = 0; i < n; i++) {
        int d = digit(nums[i], exp); // Let d be digit k of nums[i]
        counter[d]++;                // Count this occurrence of digit d
    }
    // Compute prefix sums to convert counts into array positions
    for (int i = 1; i < 10; i++) {
        counter[i] += counter[i - 1];
    }
    // Traverse backward and use the bucket counts to place each element in res
    vector<int> res(n, 0);
    for (int i = n - 1; i >= 0; i--) {
        int d = digit(nums[i], exp);
        int j = counter[d] - 1; // Get the array index j for d
        res[j] = nums[i];       // Place the current element at index j
        counter[d]--;           // Decrement the count for d by 1
    }
    // Copy the result back into nums
    for (int i = 0; i < n; i++)
        nums[i] = res[i];
}

/* Radix sort */
void radixSort(vector<int> &nums) {
    // Find the largest element to determine the maximum number of digits
    int m = *max_element(nums.begin(), nums.end());
    // Work from the least significant digit to the most significant
    for (int exp = 1; exp <= m; exp *= 10)
        // Apply counting sort to the array elements by digit k
        // k = 1 -> exp = 1
        // k = 2 -> exp = 10
        // Here exp = 10^(k-1)
        countingSortDigit(nums, exp);
}
radix_sort.java
/* Get digit k of num, where exp = 10^(k-1) */
int digit(int num, int exp) {
    // Passing exp instead of k avoids repeating the costly exponentiation here
    return (num / exp) % 10;
}

/* Counting sort by digit k of each element in nums */
void countingSortDigit(int[] nums, int exp) {
    // Decimal digits range from 0 to 9, so the bucket array needs 10 entries
    int[] counter = new int[10];
    int n = nums.length;
    // Count the occurrences of digits 0 through 9
    for (int i = 0; i < n; i++) {
        int d = digit(nums[i], exp); // Let d be digit k of nums[i]
        counter[d]++;                // Count this occurrence of digit d
    }
    // Compute prefix sums to convert counts into array positions
    for (int i = 1; i < 10; i++) {
        counter[i] += counter[i - 1];
    }
    // Traverse backward and use the bucket counts to place each element in res
    int[] res = new int[n];
    for (int i = n - 1; i >= 0; i--) {
        int d = digit(nums[i], exp);
        int j = counter[d] - 1; // Get the array index j for d
        res[j] = nums[i];       // Place the current element at index j
        counter[d]--;           // Decrement the count for d by 1
    }
    // Copy the result back into nums
    for (int i = 0; i < n; i++)
        nums[i] = res[i];
}

/* Radix sort */
void radixSort(int[] nums) {
    // Find the largest element to determine the maximum number of digits
    int m = Integer.MIN_VALUE;
    for (int num : nums)
        if (num > m)
            m = num;
    // Work from the least significant digit to the most significant
    for (int exp = 1; exp <= m; exp *= 10) {
        // Apply counting sort to the array elements by digit k
        // k = 1 -> exp = 1
        // k = 2 -> exp = 10
        // Here exp = 10^(k-1)
        countingSortDigit(nums, exp);
    }
}
radix_sort.cs
/* Get digit k of num, where exp = 10^(k-1) */
int Digit(int num, int exp) {
    // Passing exp instead of k avoids repeating the costly exponentiation here
    return (num / exp) % 10;
}

/* Counting sort by digit k of each element in nums */
void CountingSortDigit(int[] nums, int exp) {
    // Decimal digits range from 0 to 9, so the bucket array needs 10 entries
    int[] counter = new int[10];
    int n = nums.Length;
    // Count the occurrences of digits 0 through 9
    for (int i = 0; i < n; i++) {
        int d = Digit(nums[i], exp); // Let d be digit k of nums[i]
        counter[d]++;                // Count this occurrence of digit d
    }
    // Compute prefix sums to convert counts into array positions
    for (int i = 1; i < 10; i++) {
        counter[i] += counter[i - 1];
    }
    // Traverse backward and use the bucket counts to place each element in res
    int[] res = new int[n];
    for (int i = n - 1; i >= 0; i--) {
        int d = Digit(nums[i], exp);
        int j = counter[d] - 1; // Get the array index j for d
        res[j] = nums[i];       // Place the current element at index j
        counter[d]--;           // Decrement the count for d by 1
    }
    // Copy the result back into nums
    for (int i = 0; i < n; i++) {
        nums[i] = res[i];
    }
}

/* Radix sort */
void RadixSort(int[] nums) {
    // Find the largest element to determine the maximum number of digits
    int m = int.MinValue;
    foreach (int num in nums) {
        if (num > m) m = num;
    }
    // Work from the least significant digit to the most significant
    for (int exp = 1; exp <= m; exp *= 10) {
        // Apply counting sort to the array elements by digit k
        // k = 1 -> exp = 1
        // k = 2 -> exp = 10
        // Here exp = 10^(k-1)
        CountingSortDigit(nums, exp);
    }
}
radix_sort.go
/* Get digit k of num, where exp = 10^(k-1) */
func digit(num, exp int) int {
    // Passing exp instead of k avoids repeating the costly exponentiation here
    return (num / exp) % 10
}

/* Counting sort by digit k of each element in nums */
func countingSortDigit(nums []int, exp int) {
    // Decimal digits range from 0 to 9, so the bucket array needs 10 entries
    counter := make([]int, 10)
    n := len(nums)
    // Count the occurrences of digits 0 through 9
    for i := 0; i < n; i++ {
        d := digit(nums[i], exp) // Let d be digit k of nums[i]
        counter[d]++             // Count this occurrence of digit d
    }
    // Compute prefix sums to convert counts into array positions
    for i := 1; i < 10; i++ {
        counter[i] += counter[i-1]
    }
    // Traverse backward and use the bucket counts to place each element in res
    res := make([]int, n)
    for i := n - 1; i >= 0; i-- {
        d := digit(nums[i], exp)
        j := counter[d] - 1 // Get the array index j for d
        res[j] = nums[i]    // Place the current element at index j
        counter[d]--        // Decrement the count for d by 1
    }
    // Copy the result back into nums
    for i := 0; i < n; i++ {
        nums[i] = res[i]
    }
}

/* Radix sort */
func radixSort(nums []int) {
    // Find the largest element to determine the maximum number of digits
    max := math.MinInt
    for _, num := range nums {
        if num > max {
            max = num
        }
    }
    // Work from the least significant digit to the most significant
    for exp := 1; max >= exp; exp *= 10 {
        // Apply counting sort to the array elements by digit k
        // k = 1 -> exp = 1
        // k = 2 -> exp = 10
        // Here exp = 10^(k-1)
        countingSortDigit(nums, exp)
    }
}
radix_sort.swift
/* Get digit k of num, where exp = 10^(k-1) */
func digit(num: Int, exp: Int) -> Int {
    // Passing exp instead of k avoids repeating the costly exponentiation here
    (num / exp) % 10
}

/* Counting sort by digit k of each element in nums */
func countingSortDigit(nums: inout [Int], exp: Int) {
    // Decimal digits range from 0 to 9, so the bucket array needs 10 entries
    var counter = Array(repeating: 0, count: 10)
    // Count the occurrences of digits 0 through 9
    for i in nums.indices {
        let d = digit(num: nums[i], exp: exp) // Let d be digit k of nums[i]
        counter[d] += 1 // Count this occurrence of digit d
    }
    // Compute prefix sums to convert counts into array positions
    for i in 1 ..< 10 {
        counter[i] += counter[i - 1]
    }
    // Traverse backward and use the bucket counts to place each element in res
    var res = Array(repeating: 0, count: nums.count)
    for i in nums.indices.reversed() {
        let d = digit(num: nums[i], exp: exp)
        let j = counter[d] - 1 // Get the array index j for d
        res[j] = nums[i] // Place the current element at index j
        counter[d] -= 1 // Decrement the count for d by 1
    }
    // Copy the result back into nums
    for i in nums.indices {
        nums[i] = res[i]
    }
}

/* Radix sort */
func radixSort(nums: inout [Int]) {
    // Find the largest element to determine the maximum number of digits
    var m = Int.min
    for num in nums {
        if num > m {
            m = num
        }
    }
    // Work from the least significant digit to the most significant
    for exp in sequence(first: 1, next: { m >= ($0 * 10) ? $0 * 10 : nil }) {
        // Apply counting sort to the array elements by digit k
        // k = 1 -> exp = 1
        // k = 2 -> exp = 10
        // Here exp = 10^(k-1)
        countingSortDigit(nums: &nums, exp: exp)
    }
}
radix_sort.js
/* Get digit k of num, where exp = 10^(k-1) */
function digit(num, exp) {
    // Passing exp instead of k avoids repeating the costly exponentiation here
    return Math.floor(num / exp) % 10;
}

/* Counting sort by digit k of each element in nums */
function countingSortDigit(nums, exp) {
    // Decimal digits range from 0 to 9, so the bucket array needs 10 entries
    const counter = new Array(10).fill(0);
    const n = nums.length;
    // Count the occurrences of digits 0 through 9
    for (let i = 0; i < n; i++) {
        const d = digit(nums[i], exp); // Let d be digit k of nums[i]
        counter[d]++; // Count this occurrence of digit d
    }
    // Compute prefix sums to convert counts into array positions
    for (let i = 1; i < 10; i++) {
        counter[i] += counter[i - 1];
    }
    // Traverse backward and use the bucket counts to place each element in res
    const res = new Array(n).fill(0);
    for (let i = n - 1; i >= 0; i--) {
        const d = digit(nums[i], exp);
        const j = counter[d] - 1; // Get the array index j for d
        res[j] = nums[i]; // Place the current element at index j
        counter[d]--; // Decrement the count for d by 1
    }
    // Copy the result back into nums
    for (let i = 0; i < n; i++) {
        nums[i] = res[i];
    }
}

/* Radix sort */
function radixSort(nums) {
    // Find the largest element to determine the maximum number of digits
    let m = Math.max(... nums);
    // Work from the least significant digit to the most significant
    for (let exp = 1; exp <= m; exp *= 10) {
        // Apply counting sort to the array elements by digit k
        // k = 1 -> exp = 1
        // k = 2 -> exp = 10
        // Here exp = 10^(k-1)
        countingSortDigit(nums, exp);
    }
}
radix_sort.ts
/* Get digit k of num, where exp = 10^(k-1) */
function digit(num: number, exp: number): number {
    // Passing exp instead of k avoids repeating the costly exponentiation here
    return Math.floor(num / exp) % 10;
}

/* Counting sort by digit k of each element in nums */
function countingSortDigit(nums: number[], exp: number): void {
    // Decimal digits range from 0 to 9, so the bucket array needs 10 entries
    const counter = new Array(10).fill(0);
    const n = nums.length;
    // Count the occurrences of digits 0 through 9
    for (let i = 0; i < n; i++) {
        const d = digit(nums[i], exp); // Let d be digit k of nums[i]
        counter[d]++; // Count this occurrence of digit d
    }
    // Compute prefix sums to convert counts into array positions
    for (let i = 1; i < 10; i++) {
        counter[i] += counter[i - 1];
    }
    // Traverse backward and use the bucket counts to place each element in res
    const res = new Array(n).fill(0);
    for (let i = n - 1; i >= 0; i--) {
        const d = digit(nums[i], exp);
        const j = counter[d] - 1; // Get the array index j for d
        res[j] = nums[i]; // Place the current element at index j
        counter[d]--; // Decrement the count for d by 1
    }
    // Copy the result back into nums
    for (let i = 0; i < n; i++) {
        nums[i] = res[i];
    }
}

/* Radix sort */
function radixSort(nums: number[]): void {
    // Find the largest element to determine the maximum number of digits
    let m: number = Math.max(... nums);
    // Work from the least significant digit to the most significant
    for (let exp = 1; exp <= m; exp *= 10) {
        // Apply counting sort to the array elements by digit k
        // k = 1 -> exp = 1
        // k = 2 -> exp = 10
        // Here exp = 10^(k-1)
        countingSortDigit(nums, exp);
    }
}
radix_sort.dart
/* Get digit k of _num, where exp = 10^(k-1) */
int digit(int _num, int exp) {
  // Passing exp instead of k avoids repeating the costly exponentiation here
  return (_num ~/ exp) % 10;
}

/* Counting sort by digit k of each element in nums */
void countingSortDigit(List<int> nums, int exp) {
  // Decimal digits range from 0 to 9, so the bucket array needs 10 entries
  List<int> counter = List<int>.filled(10, 0);
  int n = nums.length;
  // Count the occurrences of digits 0 through 9
  for (int i = 0; i < n; i++) {
    int d = digit(nums[i], exp); // Let d be digit k of nums[i]
    counter[d]++; // Count this occurrence of digit d
  }
  // Compute prefix sums to convert counts into array positions
  for (int i = 1; i < 10; i++) {
    counter[i] += counter[i - 1];
  }
  // Traverse backward and use the bucket counts to place each element in res
  List<int> res = List<int>.filled(n, 0);
  for (int i = n - 1; i >= 0; i--) {
    int d = digit(nums[i], exp);
    int j = counter[d] - 1; // Get the array index j for d
    res[j] = nums[i]; // Place the current element at index j
    counter[d]--; // Decrement the count for d by 1
  }
  // Copy the result back into nums
  for (int i = 0; i < n; i++) nums[i] = res[i];
}

/* Radix sort */
void radixSort(List<int> nums) {
  // Find the largest element to determine the maximum number of digits
  // In Dart, int is 64 bits wide
  int m = -1 << 63;
  for (int _num in nums) if (_num > m) m = _num;
  // Work from the least significant digit to the most significant
  for (int exp = 1; exp <= m; exp *= 10)
    // Apply counting sort to the array elements by digit k
    // k = 1 -> exp = 1
    // k = 2 -> exp = 10
    // Here exp = 10^(k-1)
    countingSortDigit(nums, exp);
}
radix_sort.rs
/* Get digit k of num, where exp = 10^(k-1) */
fn digit(num: i32, exp: i32) -> usize {
    // Passing exp instead of k avoids repeating the costly exponentiation here
    return ((num / exp) % 10) as usize;
}

/* Counting sort by digit k of each element in nums */
fn counting_sort_digit(nums: &mut [i32], exp: i32) {
    // Decimal digits range from 0 to 9, so the bucket array needs 10 entries
    let mut counter = [0; 10];
    let n = nums.len();
    // Count the occurrences of digits 0 through 9
    for i in 0..n {
        let d = digit(nums[i], exp); // Let d be digit k of nums[i]
        counter[d] += 1; // Count this occurrence of digit d
    }
    // Compute prefix sums to convert counts into array positions
    for i in 1..10 {
        counter[i] += counter[i - 1];
    }
    // Traverse backward and use the bucket counts to place each element in res
    let mut res = vec![0; n];
    for i in (0..n).rev() {
        let d = digit(nums[i], exp);
        let j = counter[d] - 1; // Get the array index j for d
        res[j] = nums[i]; // Place the current element at index j
        counter[d] -= 1; // Decrement the count for d by 1
    }
    // Copy the result back into nums
    nums.copy_from_slice(&res);
}

/* Radix sort */
fn radix_sort(nums: &mut [i32]) {
    // Find the largest element to determine the maximum number of digits
    let m = *nums.into_iter().max().unwrap();
    // Work from the least significant digit to the most significant
    let mut exp = 1;
    while exp <= m {
        counting_sort_digit(nums, exp);
        exp *= 10;
    }
}
radix_sort.c
/* Get digit k of num, where exp = 10^(k-1) */
int digit(int num, int exp) {
    // Passing exp instead of k avoids repeating the costly exponentiation here
    return (num / exp) % 10;
}

/* Counting sort by digit k of each element in nums */
void countingSortDigit(int nums[], int size, int exp) {
    // Decimal digits range from 0 to 9, so the bucket array needs 10 entries
    int *counter = (int *)malloc((sizeof(int) * 10));
    memset(counter, 0, sizeof(int) * 10); // Initialize to 0 so the memory can be freed later
    // Count the occurrences of digits 0 through 9
    for (int i = 0; i < size; i++) {
        // Let d be digit k of nums[i]
        int d = digit(nums[i], exp);
        // Count this occurrence of digit d
        counter[d]++;
    }
    // Compute prefix sums to convert counts into array positions
    for (int i = 1; i < 10; i++) {
        counter[i] += counter[i - 1];
    }
    // Traverse backward and use the bucket counts to place each element in res
    int *res = (int *)malloc(sizeof(int) * size);
    for (int i = size - 1; i >= 0; i--) {
        int d = digit(nums[i], exp);
        int j = counter[d] - 1; // Get the array index j for d
        res[j] = nums[i];       // Place the current element at index j
        counter[d]--;           // Decrement the count for d by 1
    }
    // Copy the result back into nums
    for (int i = 0; i < size; i++) {
        nums[i] = res[i];
    }
    // Free memory
    free(res);
    free(counter);
}

/* Radix sort */
void radixSort(int nums[], int size) {
    // Find the largest element to determine the maximum number of digits
    int max = INT32_MIN;
    for (int i = 0; i < size; i++) {
        if (nums[i] > max) {
            max = nums[i];
        }
    }
    // Work from the least significant digit to the most significant
    for (int exp = 1; max >= exp; exp *= 10)
        // Apply counting sort to the array elements by digit k
        // k = 1 -> exp = 1
        // k = 2 -> exp = 10
        // Here exp = 10^(k-1)
        countingSortDigit(nums, size, exp);
}
radix_sort.kt
/* Get digit k of num, where exp = 10^(k-1) */
fun digit(num: Int, exp: Int): Int {
    // Passing exp instead of k avoids repeating the costly exponentiation here
    return (num / exp) % 10
}

/* Counting sort by digit k of each element in nums */
fun countingSortDigit(nums: IntArray, exp: Int) {
    // Decimal digits range from 0 to 9, so the bucket array needs 10 entries
    val counter = IntArray(10)
    val n = nums.size
    // Count the occurrences of digits 0 through 9
    for (i in 0..<n) {
        val d = digit(nums[i], exp) // Let d be digit k of nums[i]
        counter[d]++                // Count this occurrence of digit d
    }
    // Compute prefix sums to convert counts into array positions
    for (i in 1..9) {
        counter[i] += counter[i - 1]
    }
    // Traverse backward and use the bucket counts to place each element in res
    val res = IntArray(n)
    for (i in n - 1 downTo 0) {
        val d = digit(nums[i], exp)
        val j = counter[d] - 1 // Get the array index j for d
        res[j] = nums[i]       // Place the current element at index j
        counter[d]--           // Decrement the count for d by 1
    }
    // Copy the result back into nums
    for (i in 0..<n)
        nums[i] = res[i]
}

/* Radix sort */
fun radixSort(nums: IntArray) {
    // Find the largest element to determine the maximum number of digits
    var m = Int.MIN_VALUE
    for (num in nums) if (num > m) m = num
    var exp = 1
    // Work from the least significant digit to the most significant
    while (exp <= m) {
        // Apply counting sort to the array elements by digit k
        // k = 1 -> exp = 1
        // k = 2 -> exp = 10
        // Here exp = 10^(k-1)
        countingSortDigit(nums, exp)
        exp *= 10
    }
}
radix_sort.rb
### Get digit k of num, where exp = 10^(k-1) ###
def digit(num, exp)
  # Passing exp instead of k avoids repeating the costly exponentiation here
  (num / exp) % 10
end

### Counting sort by digit k of each element in nums###
def counting_sort_digit(nums, exp)
  # Decimal digits range from 0 to 9, so the bucket array needs 10 entries
  counter = Array.new(10, 0)
  n = nums.length
  # Count the occurrences of digits 0 through 9
  for i in 0...n
    d = digit(nums[i], exp) # Let d be digit k of nums[i]
    counter[d] += 1 # Count this occurrence of digit d
  end
  # Compute prefix sums to convert counts into array positions
  (1...10).each { |i| counter[i] += counter[i - 1] }
  # Traverse backward and use the bucket counts to place each element in res
  res = Array.new(n, 0)
  for i in (n - 1).downto(0)
    d = digit(nums[i], exp)
    j = counter[d] - 1 # Get the array index j for d
    res[j] = nums[i] # Place the current element at index j
    counter[d] -= 1 # Decrement the count for d by 1
  end
  # Copy the result back into nums
  (0...n).each { |i| nums[i] = res[i] }
end

### Radix sort ###
def radix_sort(nums)
  # Find the largest element to determine the maximum number of digits
  m = nums.max
  # Work from the least significant digit to the most significant
  exp = 1
  while exp <= m
    # Apply counting sort to the array elements by digit k
    # k = 1 -> exp = 1
    # k = 2 -> exp = 10
    # Here exp = 10^(k-1)
    counting_sort_digit(nums, exp)
    exp *= 10
  end
end
Visualize execution

Why start with the least significant digit?

Each pass takes precedence over the one before it. For example, if the first pass orders two elements as \(a < b\) but the second orders them as \(a > b\), the second order prevails. Since more significant digits have higher priority, sort by the less significant digits first.

11.10.2   Algorithm properties

Compared with counting sort, radix sort handles a larger range of values, provided that the data can be represented with a fixed number of digits and that this number is not too large. For example, floating-point numbers are a poor fit here because their digit count \(k\) can be so large that the time complexity satisfies \(O(nk) \gg O(n^2)\).

  • Time complexity: \(O(nk)\), nonadaptive. Suppose there are \(n\) elements in base \(d\), each with at most \(k\) digits. Counting sort takes \(O(n + d)\) time for one digit, so sorting by all \(k\) digits takes \(O((n + d)k)\) time. Typically, \(d\) and \(k\) are both small and the time complexity approaches \(O(n)\).
  • Space complexity: \(O(n + d)\), out-of-place. As in counting sort, radix sort uses an array res of length \(n\) and an array counter of length \(d\).
  • Stable. If the counting sort used for each digit is stable, radix sort is stable too. If counting sort is unstable, radix sort may produce an incorrect result.
Share your thoughts, questions, or suggestions in the comments.