Radix Sort Algorithm in DSA Python - Time & Space Complexity
We want to understand how the time taken by Radix Sort changes as the list size grows.
Specifically, how the sorting steps add up when sorting numbers digit by digit.
Analyze the time complexity of the following Radix Sort code snippet.
def counting_sort(arr, exp):
n = len(arr)
output = [0] * n
count = [0] * 10
for i in range(n):
index = (arr[i] // exp) % 10
count[index] += 1
for i in range(1, 10):
count[i] += count[i - 1]
i = n - 1
while i >= 0:
index = (arr[i] // exp) % 10
output[count[index] - 1] = arr[i]
count[index] -= 1
i -= 1
for i in range(n):
arr[i] = output[i]
def radix_sort(arr):
max_num = max(arr)
exp = 1
while max_num // exp > 0:
counting_sort(arr, exp)
exp *= 10
This code sorts numbers by processing each digit from least to most significant using counting sort.
Look at what repeats as input grows.
- Primary operation: Counting sort runs once for each digit place.
- How many times: Number of digits in the largest number (d times).
- Each counting sort loops over all n elements multiple times.
For each digit, we do a counting sort over all n elements.
| Input Size (n) | Approx. Operations (d * n) |
|---|---|
| 10 | 10 * d |
| 100 | 100 * d |
| 1000 | 1000 * d |
As n grows, operations grow linearly with n and also depend on d, the digit count.
Time Complexity: O(d * n)
This means the time grows linearly with the number of elements and the number of digits in the largest number.
[X] Wrong: "Radix sort is always faster than comparison sorts like quicksort because it doesn't compare elements."
[OK] Correct: Radix sort depends on digit count d; if d is large, it can be slower. Also, it needs extra space and works best on fixed-length numbers.
Understanding Radix Sort's time complexity helps you explain when and why to use it, showing you know both algorithm steps and efficiency.
"What if the numbers had varying digit lengths but we padded them to the same length? How would that affect the time complexity?"
Practice
Radix Sort algorithm?Solution
Step 1: Understand Radix Sort approach
Radix Sort sorts numbers by processing individual digits starting from the least significant digit (rightmost).Step 2: Compare with other sorting methods
Other options describe Bubble Sort, Merge Sort, and Quick Sort, which are different algorithms.Final Answer:
Sort numbers digit by digit starting from the least significant digit -> Option DQuick Check:
Radix Sort = Sort by digits from right to left [OK]
- Confusing Radix Sort with comparison-based sorts
- Thinking it sorts from most significant digit first
- Assuming it uses pivot or recursion
Solution
Step 1: Understand bucket initialization
We need 10 separate empty lists (buckets) for digits 0-9, so list comprehension creates independent lists.Step 2: Analyze options
buckets = [[]] * 10 creates 10 references to the same list, causing bugs. buckets = [0] * 10 creates integers, not lists. buckets = list(range(10)) creates a list of numbers, not buckets.Final Answer:
buckets = [[] for _ in range(10)] -> Option BQuick Check:
Independent empty lists = list comprehension [OK]
- Using [[]] * 10 causes all buckets to reference the same list
- Initializing buckets with zeros instead of lists
- Confusing range with list of empty lists
arr = [170, 45, 75, 90, 802, 24, 2, 66]
buckets = [[] for _ in range(10)]
for num in arr:
digit = num % 10
buckets[digit].append(num)
result = []
for bucket in buckets:
result.extend(bucket)
print(result)Solution
Step 1: Group numbers by least significant digit
Digits are: 170(0), 45(5), 75(5), 90(0), 802(2), 24(4), 2(2), 66(6). Buckets: 0:[170,90], 2:[802,2], 4:[24], 5:[45,75], 6:[66].Step 2: Concatenate buckets in order 0 to 9
Result list is [170, 90] + [] + [802, 2] + [] + [24] + [45, 75] + [66] + [] + [] + [] = [170, 90, 802, 2, 24, 45, 75, 66].Final Answer:
[170, 90, 802, 2, 24, 45, 75, 66] -> Option CQuick Check:
Sorted by last digit = [170, 90, 802, 2, 24, 45, 75, 66] [OK]
- Mixing digit extraction (using // instead of %)
- Not preserving order inside buckets
- Concatenating buckets in wrong order
def radix_sort(arr):
max_num = max(arr)
exp = 1
while max_num // exp > 0:
buckets = [[]] * 10
for num in arr:
index = (num // exp) % 10
buckets[index].append(num)
arr = []
for bucket in buckets:
arr.extend(bucket)
exp *= 10
return arr
print(radix_sort([170, 45, 75, 90, 802, 24, 2, 66]))Solution
Step 1: Identify bucket initialization issue
buckets = [[]] * 10 creates 10 references to the same list, so appending to one bucket appends to all.Step 2: Check other parts
Loop condition and digit extraction are correct. The function returns arr properly.Final Answer:
Using buckets = [[]] * 10 causes all buckets to reference the same list -> Option AQuick Check:
Bucket initialization = [[] for _ in range(10)] [OK]
- Using [[]]*10 instead of list comprehension
- Changing loop condition incorrectly
- Misunderstanding digit extraction
[329, 457, 657, 839, 436, 720, 355] using Radix Sort. After sorting by the least significant digit (units place), what will the list look like?Solution
Step 1: Extract least significant digit of each number
329(9), 457(7), 657(7), 839(9), 436(6), 720(0), 355(5).Step 2: Group numbers by last digit and concatenate in digit order 0-9
Digits 0:[720], 5:[355], 6:[436], 7:[457, 657], 9:[329, 839]. Concatenate: [720, 355, 436, 457, 657, 329, 839].Final Answer:
[720, 355, 436, 457, 657, 329, 839] -> Option AQuick Check:
Sorted by units digit = [720, 355, 436, 457, 657, 329, 839] [OK]
- Mixing digit extraction with division instead of modulo
- Not preserving order of numbers with same digit
- Concatenating buckets in wrong digit order
Ready to prove it?
Pass the test and this pattern moves unknown
