Radix Sort Algorithm in DSA C - Time & Space Complexity
We want to understand how the time needed by Radix Sort changes as the list size grows.
Specifically, how does the sorting time increase when we have more numbers or bigger numbers?
Analyze the time complexity of the following code snippet.
void radixSort(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) max = arr[i];
}
for (int exp = 1; max / exp > 0; exp *= 10) {
countingSortByDigit(arr, n, exp);
}
}
This code finds the largest number, then sorts the array digit by digit using counting sort.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The outer loop runs once for each digit place (ones, tens, hundreds, etc.).
- How many times: Number of digits in the largest number (d times).
- Inside each iteration: Counting sort runs over all n elements.
As the number of elements (n) grows, counting sort runs on all elements for each digit.
| Input Size (n) | Approx. Operations (d * n) |
|---|---|
| 10 | About 30 (if max has 3 digits) |
| 100 | About 300 |
| 1000 | About 3000 |
Pattern observation: Operations grow roughly in a straight line with n, multiplied by the number of digits d.
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 always runs in linear time O(n)."
[OK] Correct: The time also depends on d, the number of digits. If numbers are very large, d grows and so does time.
Understanding Radix Sort's time helps you explain when it is faster than comparison sorts and why digit count matters.
"What if we used a base other than 10 for digit grouping? How would that affect the time complexity?"
Practice
Radix Sort Algorithm?Solution
Step 1: Understand Radix Sort process
Radix Sort sorts numbers by processing each digit from least significant to most significant.Step 2: Compare with other sorting methods
Unlike comparison-based sorts, Radix Sort uses digit positions to sort without direct comparisons.Final Answer:
Sort numbers digit by digit starting from the least significant digit -> Option CQuick Check:
Radix Sort = Digit by digit sorting [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 counting array role
The counting array stores frequency of digits 0-9 and must start at zero.Step 2: Check initialization options
Onlyint count[10] = {0};sets all elements to zero correctly in C.Final Answer:
int count[10] = {0}; -> Option AQuick Check:
Counting array must start with zeros [OK]
- Leaving counting array uninitialized
- Initializing with all ones
- Assigning NULL to an int array
arr = {170, 45, 75, 90, 802, 24, 2, 66}?
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
int n = sizeof(arr)/sizeof(arr[0]);
radixSort(arr, n); // Assume radixSort is correctly implemented
printArray(arr, n);
return 0;
}Solution
Step 1: Understand Radix Sort output
Radix Sort sorts the array fully in ascending order after processing all digits.Step 2: Sort given array
Sorted array is [2, 24, 45, 66, 75, 90, 170, 802].Final Answer:
2 24 45 66 75 90 170 802 -> Option DQuick Check:
Sorted array matches 2 24 45 66 75 90 170 802 [OK]
- Assuming output is unchanged
- Thinking output is reversed
- Confusing partial digit sorting with final output
for (int i = 0; i < n; i++) {
count[(arr[i] / exp) % 10]++;
}
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10]] = arr[i];
count[(arr[i] / exp) % 10]--;
}Solution
Step 1: Analyze output array indexing
Indexing output array must use count[...] - 1 because count stores cumulative counts.Step 2: Check other parts
Counting array size 10 is correct for digits 0-9; prefix sum loop starts at 1; division by exp is correct.Final Answer:
Indexing output array should be count[...] - 1 -> Option BQuick Check:
Output index = count - 1 [OK]
- Using count directly as index without subtracting 1
- Wrong counting array size
- Incorrect loop start for prefix sums
- Misusing division and modulo operators
["002", "010", "001", "100"], how can Radix Sort be adapted to sort them correctly as numbers?Solution
Step 1: Understand string number sorting
Strings with leading zeros must be equal length to compare digits correctly.Step 2: Adapt Radix Sort
Pad strings to same length, then sort digit by digit from right to left to preserve numeric order.Final Answer:
Pad all strings to equal length and sort digit by digit from right to left -> Option AQuick Check:
Equal length padding + digit-wise sort = correct numeric order [OK]
- Sorting strings lexicographically without padding
- Converting to int loses leading zeros importance
- Sorting only first character causes wrong order
Ready to prove it?
Pass the test and this pattern moves unknown
