-
Hajipur, Bihar, 844101
Radix sort is a powerful non-comparison sorting algorithm that sorts numbers digit by digit. Instead of comparing elements directly, it groups numbers based on each digit starting from the least significant digit (LSD) or the most significant digit (MSD). Radix sort becomes extremely efficient when dealing with large lists of integers that have a similar number of digits.
In this tutorial, you’ll learn how radix sort works, the difference between LSD and MSD methods, how counting sort is used inside radix sort, how to implement the algorithm in Python and where it is used in real-world systems. Understanding radix sort helps you move toward advanced sorting logic and improves your mental model for data processing.
Radix sort is a digit-based sorting algorithm that processes numbers one digit at a time. It usually uses counting sort as a helper algorithm to sort the digits at each position.
The core idea is simple:
Sort numbers by 1s digit
Then sort by 10s digit
Then sort by 100s digit
Continue until the largest digit place is processed
After going through all digit places, the list becomes completely sorted.
Let’s use this list:
[170, 45, 75, 90, 802, 24, 2, 66]
Result after sorting by unit digit:
[170, 90, 802, 2, 24, 45, 75, 66]
Using the above result:
[802, 2, 24, 45, 66, 170, 75, 90]
[2, 24, 45, 66, 75, 90, 170, 802]
After all digit places are processed, the list becomes completely sorted.
Radix sort offers some strong benefits:
Runs in linear time O(nk)
Works extremely well for numbers with equal digit lengths
No element-to-element comparisons
Stable sorting
Faster than comparison-based sorts for certain datasets
Because of its efficiency, radix sort is widely used in areas where speed matters and input values follow a predictable structure.
Radix sort uses counting sort internally. Here is the complete implementation:
def counting_sort_exp(data, exp):
n = len(data)
output = [0] * n
count = [0] * 10
for num in data:
index = (num // exp) % 10
count[index] += 1
for i in range(1, 10):
count[i] += count[i - 1]
for i in range(n - 1, -1, -1):
index = (data[i] // exp) % 10
output[count[index] - 1] = data[i]
count[index] -= 1
return output
def radix_sort(data):
if not data:
return data
max_val = max(data)
exp = 1
while max_val // exp > 0:
data = counting_sort_exp(data, exp)
exp *= 10
return data
This version processes digits from lowest place to highest place.
numbers = [170, 45, 75, 90, 802, 24, 2, 66]
print(radix_sort(numbers))
Output:
[2, 24, 45, 66, 75, 90, 170, 802]
This shows how each digit pass gradually organizes the list.
Radix sort’s time depends on:
n = number of elements
k = number of digits in the largest number
The complexity is:
Best case: O(nk)
Average case: O(nk)
Worst case: O(nk)
This performance can outperform comparison sort which cannot go below O(n log n).
Use radix sort when:
Sorting integers
Numbers share a similar number of digits
Values have fixed length (like IDs, phone numbers, roll numbers)
You want stable and predictable performance
You need high speed for large datasets
This makes radix sort a good choice for large structured numeric data.
Avoid radix sort when:
Values have very large digit lengths
Data cannot easily break into digits
Memory for counting arrays is limited
Comparison-based sorting is more practical
For datasets with huge ranges or mixed data types, quick sort or merge sort works better.
Radix sort appears in many systems where large numeric datasets are sorted frequently:
Indexes sorted by numeric keys use digit-wise logic.
Fixed-length digits make sorting straightforward.
Web platforms often store large structured IDs.
Postal systems often use digit-based sorting.
These structures use digit or character ordering internally.
List:
[121, 432, 564, 23, 1, 45, 788]
Step 1: Sort by 1s place
Step 2: Sort by 10s place
Step 3: Sort by 100s place
Final sorted output:
[1, 23, 45, 121, 432, 564, 788]
Radix sort repeats counting sort on digit positions until the largest one is covered.
print(radix_sort([432, 8, 530, 90, 88]))
rolls = [112, 59, 203, 8, 15]
print(radix_sort(rolls))
ids = [5012, 4021, 8999, 1020]
print(radix_sort(ids))
print(radix_sort([3, 1, 2]))
print(radix_sort([22, 11, 22, 33]))
print(radix_sort([19, 2, 45, 11, 9]))
data = [100, 999, 120, 450]
print(radix_sort(data))
print(radix_sort([0, 10, 5, 100]))
print(radix_sort([1, 2, 3, 4, 5]))
print(radix_sort([9, 8, 7, 6, 5]))
Radix sort works by sorting numbers digit by digit using counting sort as a helper algorithm. You learned how the algorithm processes each digit place, why it becomes fast for large datasets with fixed digit lengths and where it is commonly used in real-world applications. Radix sort is a key algorithm for dealing with structured numeric data efficiently.
Q1. Write a Python program to sort the list [34, 2, 122, 98, 1] using radix sort.
Q2. Write a Python program to modify radix sort to print the array after each digit pass.
Q3. Write a Python program to generate a random list of 20 integers and sort it using radix sort.
Q4. Write a Python program to count and display the total number of digit passes required in radix sort.
Q5. Write a Python program to accept a list of integers from user input and sort it using radix sort.
Q6. Write a Python program to test radix sort on numbers up to 100000 and display the sorted output.
Q7. Write a Python program to add a check for an empty list before performing radix sort.
Q8. Write a Python program to modify radix sort to work using base-16 (hexadecimal digits).
Q9. Write a Python program to compare the output of radix sort with the built-in sorted() function.
Q10. Write a Python program to implement radix sort by treating numbers as strings of digits.