Back to Data Structures & Algorithms
2025-12-316 min read

Radix Sort (Data Structures & Algorithms)

Learn Radix Sort (Data Structures & Algorithms) step by step with clear examples and exercises.

Why This Matters

Understanding Radix Sort is crucial in various contexts, including competitive programming, coding interviews, and real-world scenarios where efficiency matters, especially when dealing with large data sets that have base-specific properties such as phone numbers or IDs. Its simplicity, efficiency, and stability make it a valuable tool for sorting such data.

Prerequisites

Before diving into Radix Sort, you should have a solid understanding of the following Python concepts:

  1. Basic Python data structures (lists, tuples, and dictionaries)
  2. Loops (for loops and while loops)
  3. Conditional statements (if-else)
  4. Functions in Python
  5. Understanding of Big O notation for time complexity analysis
  6. Knowledge of basic sorting algorithms like Bubble Sort, Selection Sort, Insertion Sort, QuickSort, MergeSort, and HeapSort
  7. Familiarity with string manipulation techniques in Python
  8. Comprehension of data types and their conversions (e.g., integer, float, string)

Core Concept

Radix sort is a non-comparative sorting algorithm that sorts elements based on the number of digits (or characters) and their positions within each digit group. It's an efficient, stable, and simple sorting method for large data sets with base-specific properties.

Steps in Radix Sort:

  1. Determine the maximum number of digits (or characters) in any element.
  2. Perform passes from the least significant digit to the most significant digit, sorting elements within each digit group using a counting sort or bucket sort.
  3. Repeat step 2 for all digit groups until all elements are sorted.

Example with Base-10 Numbers:

Consider an array of numbers [735, 894, 562, 21, 97]. The maximum number of digits is 3 (for number 894). We'll perform three passes, each focusing on a specific digit group.

Pass 1: LSD (Least Significant Digit)

  • Create ten buckets (one for each digit from 0 to 9).
  • For each number in the array, place it in the appropriate bucket based on its least significant digit.
  • After all numbers have been placed in their respective buckets, rebuild the sorted array by concatenating the elements in each bucket from left to right.

Pass 2: MSD (Second Least Significant Digit)

  • Perform the same process as Pass 1 but for the second least significant digit.

Pass 3: MSF (Most Significant Digit)

  • Perform the same process as Pass 1 but for the most significant digit.

After three passes, the array [735, 894, 562, 21, 97] will be sorted in ascending order: [21, 562, 735, 894, 97].

Worked Example

Let's implement Radix Sort for a list of phone numbers.

def radix_sort(numbers):
max_length = len(str(max(numbers)))

for i in range(max_length):
buckets = [[] for _ in range(10)]
negatives = []

for number in numbers:
if number < 0:
negatives.append(-number)
else:
digit = (number // 10**i) % 10
buckets[digit].append(number)

numbers[:] = []
for bucket in buckets:
numbers += sorted(bucket)

if negatives:
numbers += sorted(negatives)

return numbers

numbers = [48392765, 32691577, 46802538, 77812098, 24783617]
print(radix_sort(numbers)) # Output: [24783617, 32691577, 46802538, 48392765, 77812098]

In this example, we've created a function radix_sort() that takes a list of phone numbers and returns the sorted list using Radix Sort. The function handles negative numbers by separating them from positive numbers and sorting them after the positive numbers have been sorted.

Common Mistakes

1. Forgetting to handle leading zeros

When dealing with numbers that have varying lengths and leading zeros, it's essential to pad all numbers with the same number of leading zeros so that they have equal length during each pass. This can be done by converting all numbers into strings, padding them with leading zeros if necessary, and then converting them back to integers before performing the sorting operations.

2. Not considering negative numbers

Radix sort assumes all elements are positive integers. To handle negative numbers, you can first convert them into their absolute values and then add the original signs back after sorting. You should also remember to pad negative numbers with an extra digit (e.g., -3 becomes -0003) so that they have the same length as positive numbers during each pass.

3. Using a naive implementation of counting sort or bucket sort during passes

During each pass, it's crucial to use an optimized version of counting sort or bucket sort for efficient processing. Counting sort can be optimized by using a single array to store counts and another array to accumulate sorted elements. Bucket sort can also be optimized by merging smaller arrays into larger ones during the sorting process.

Practice Questions

  1. Implement Radix Sort for base-256 numbers (ASCII characters).
  2. Write a function that sorts a list of IP addresses using Radix Sort. Hint: An IP address can be represented as four integers between 0 and 255, separated by dots.
  3. Modify the Radix Sort implementation to handle negative numbers correctly.
  4. Optimize the Radix Sort implementation by using an optimized version of counting sort or bucket sort during passes.
  5. Compare the time complexity and efficiency of Radix Sort with other popular sorting algorithms like QuickSort, MergeSort, and HeapSort in various scenarios (e.g., sorted, reverse-sorted, random).
  6. Implement a variation of Radix Sort called LSD Radix Sort that only sorts the least significant digits (or characters) and uses an external sorting algorithm for the remaining digits (or characters).
  7. Analyze the space complexity of Radix Sort and discuss potential solutions to reduce it when dealing with extremely large data sets.
  8. Implement a function that takes a list of mixed data types (e.g., integers, strings) and sorts them using Radix Sort after converting all elements into their appropriate data type (integer or string).
  9. Extend the Radix Sort implementation to handle multi-key sorting, where each element has multiple keys (e.g., a student record with name, ID, and grade).
  10. Implement a version of Radix Sort that can handle arbitrary bases (not limited to base-10) for sorting numbers in different number systems.

FAQ

What is the time complexity of Radix Sort?

Radix Sort has a worst-case and average time complexity of O(nk), where n is the number of elements and k is the maximum number of digits (or characters) in any element. In practice, it performs well for large data sets with base-specific properties.

Why is Radix Sort stable?

Radix Sort is a stable sorting algorithm because it compares elements based on their positions within each digit group instead of comparing the elements themselves. This means that equal elements maintain their relative order throughout the sorting process.

Can Radix Sort handle negative numbers?

Yes, Radix Sort can handle negative numbers by converting them into their absolute values during the sorting process and then adding the original signs back after sorting. However, it's important to pad negative numbers with an extra digit (e.g., -3 becomes -0003) so that they have the same length as positive numbers during each pass.

How does Radix Sort compare to other sorting algorithms in terms of efficiency?

Radix Sort is generally more efficient than comparison-based sorting algorithms like Bubble Sort, Selection Sort, and Insertion Sort for large data sets with base-specific properties. However, it may not be as efficient as QuickSort, MergeSort, or HeapSort for sorted, reverse-sorted, or random data sets due to its fixed O(nk) time complexity.

Can Radix Sort handle multi-key sorting?

Yes, Radix Sort can be extended to handle multi-key sorting by sorting each key separately and then combining the sorted results based on the desired order of the keys. This can be useful for sorting records with multiple attributes (e.g., student records with name, ID, and grade).

Radix Sort (Data Structures & Algorithms) | Data Structures & Algorithms | XQA Learn