Understanding Radix Sort in Detail
Radix Sort is a non-comparative integer sorting algorithm. Unlike Merge Sort or Quick Sort, which sort by comparing two elements at a time, Radix Sort works by processing the individual digits of the numbers. It sorts numbers digit by digit, usually starting from the least significant digit (LSD) to the most significant digit (MSD).
Think of how you might organize a large stack of dates (DD/MM/YYYY). You wouldn't compare every date to every other date. Instead, you might first sort them into piles by Day (1-31). Then, you gather them up and sort them by Month (1-12), preserving the order of days. Finally, you sort by Year. By the end, the entire stack is sorted chronologically. This is exactly how Radix Sort works!
The Logic Behind the Digits
Radix Sort uses a stable sorting algorithm (usually Counting Sort) as a subroutine to sort by each digit place.
- Find Max: Determine the maximum number to know the number of digits
dwe need to process. - Iterate Digits: Start with the least significant digit (1s place). Sort the entire array based ONLY on this digit using a stable sort.
- Move Left: Move to the next significant digit (10s place) and sort again, preserving the relative order of items with the same 10s digit.
- Repeat: Continue this process until the most significant digit is processed.
Step-by-Step Algorithm Trace
Let's trace the algorithm on a small array using LSD (Least Significant Digit) Radix Sort.
Input Array: [170, 45, 75, 90, 24, 2]
Max = 170 (3 digits)
Pass 1: Sort by Units (1s)
- We look at the last digit: 170, 45, 75, 90, 24, 2.
- Sort based on this digit (0-9).
- Order: 170, 90 (ends in 0), 2 (ends in 2), 24 (ends in 4), 45, 75 (ends in 5).
Result: [170, 90, 2, 24, 45, 75]
Pass 2: Sort by Tens (10s)
- We look at the middle digit (pad with 0 if needed): 170, 90, 02, 24, 45, 75.
- Sort based on this digit.
- Order: 02 (0), 24 (2), 45 (4), 170 (7), 75 (7), 90 (9).
- Note: 170 came before 75 because it was before 75 in the previous step (Stability).
Result: [2, 24, 45, 170, 75, 90]
Pass 3: Sort by Hundreds (100s)
- We look at the first digit: 170, 090, 002, 024, 045, 075.
- Sort based on this digit.
- Order: 2, 24, 45, 75, 90 (all start with 0), 170 (starts with 1).
Final Sorted Array: [2, 24, 45, 75, 90, 170]
Code Implementation
Below is the implementation of Radix Sort in multiple programming languages.
#include <stdio.h>#include <stdlib.h>int getMax(int arr[], int n) {int max = arr[0];for (int i = 1; i < n; i++)if (arr[i] > max) max = arr[i];return max;}void countSort(int arr[], int n, int exp) {int* output = (int*)malloc(n * sizeof(int));int count[10] = {0};// Count occurrences based on digitfor (int i = 0; i < n; i++)count[(arr[i] / exp) % 10]++;// Compute cumulative countfor (int i = 1; i < 10; i++)count[i] += count[i - 1];// Build output arrayfor (int i = n - 1; i >= 0; i--) {output[count[(arr[i] / exp) % 10] - 1] = arr[i];count[(arr[i] / exp) % 10]--;}for (int i = 0; i < n; i++)arr[i] = output[i];free(output);}void radixSort(int arr[], int n) {int max = getMax(arr, n);// Apply counting sort for each digitfor (int exp = 1; max / exp > 0; exp *= 10)countSort(arr, n, exp);}int main() {int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};int n = sizeof(arr) / sizeof(arr[0]);radixSort(arr, n);printf("Sorted array: ");for (int i = 0; i < n; i++)printf("%d ", arr[i]);return 0;}
Time & Space Complexity Analysis
Radix Sort can be faster than comparison sorts for large lists of integers, but it depends heavily on the number of digits.
Time Complexity
Where d is the number of digits, n is the number of elements, and k is the base (usually 10). If d is constant and small, this is effectively linear time O(n).
Space Complexity
Requires auxiliary space for the counting sort subroutine.
When to Use Radix Sort?
Radix Sort is specialized for specific data types and constraints.
Fixed-Length Keys
It shines when sorting data with fixed lengths, such as IP addresses, phone numbers, or ID numbers.
Large Numbers vs. Large Range
Radix Sort is better than Counting Sort when the range of numbers is huge (e.g., 1 to 1,000,000,000) but the number of digits is manageable. Counting Sort would crash your memory trying to create a billion-sized array, but Radix Sort only needs a size-10 array, reused 10 times.
Parallel Processing
Radix Sort is easier to parallelize than Merge Sort or Quick Sort, making it popular for sorting algorithms running on GPUs (Graphics Processing Units).