Radix Sort Algorithm in DSA C++ - Time & Space Complexity
We want to understand how the time taken by Radix Sort changes as the list size grows.
How does the number of steps grow when sorting more numbers?
Analyze the time complexity of the following code snippet.
void radixSort(int arr[], int n) {
int maxVal = *max_element(arr, arr + n);
for (int exp = 1; maxVal / exp > 0; exp *= 10) {
int output[n], count[10] = {0};
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] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
for (int i = 0; i < n; i++)
arr[i] = output[i];
}
}
This code sorts an array of integers by processing digits from least to most significant.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The outer loop runs once per digit place (like ones, tens, hundreds).
- How many times: For each digit, it loops over the entire array multiple times to count and reorder.
Execution grows with both the number of elements and the number of digits in the largest number.
| Input Size (n) | Approx. Operations (for d digits) |
|---|---|
| 10 | About 10 x d steps |
| 100 | About 100 x d steps |
| 1000 | About 1000 x d steps |
Pattern observation: The steps grow linearly with the number of elements and digits.
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 the number of digits (d), so if numbers have many digits, it takes longer.
Understanding Radix Sort's time complexity helps you explain when it is efficient and how it compares to other sorting methods.
"What if we changed the base from 10 to 2 (binary digits)? How would the time complexity change?"
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) to the most significant digit (leftmost).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 AQuick 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 swapping of elements
pos (starting from 0 for least significant digit) from an integer num in C++?Solution
Step 1: Understand digit extraction formula
To get the digit at position pos, divide num by 10^pos and then take modulo 10.Step 2: Check each option
((num / (int)pow(10, pos)) % 10 correctly uses pow(10, pos) for exponentiation and modulo 10 to get the digit.Final Answer:
((num / (int)pow(10, pos)) % 10) -> Option BQuick Check:
Digit extraction = (num / 10^pos) % 10 [OK]
- Using multiplication instead of exponentiation
- Incorrect order of operations
- Using modulo before division
arr = {170, 45, 75, 90, 802, 24, 2, 66}?
void countingSort(int arr[], int n, int exp) {
int output[n];
int count[10] = {0};
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] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
for (int i = 0; i < n; i++)
arr[i] = output[i];
}
void radixSort(int arr[], int n) {
int maxVal = *max_element(arr, arr + n);
for (int exp = 1; maxVal / exp > 0; exp *= 10)
countingSort(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);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}Solution
Step 1: Understand Radix Sort steps on given array
Radix Sort sorts by digits: first units, then tens, then hundreds. After full sorting, array becomes sorted ascending.Step 2: Trace final sorted array
Sorted array is: 2 24 45 66 75 90 170 802.Final Answer:
2 24 45 66 75 90 170 802 -> Option CQuick Check:
Radix Sort final output = sorted array ascending [OK]
- Confusing intermediate steps with final output
- Misordering numbers with same digits
- Ignoring stable sorting in countingSort
void countingSort(int arr[], int n, int exp) {
int output[n];
int count[10] = {0};
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 = 0; i < n; 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];
}Solution
Step 1: Identify stable sorting requirement in counting sort
Counting sort must process elements from right to left to maintain stability.Step 2: Check the for loop direction
Current loop goes from 0 to n-1, which breaks stability. Changing it to n-1 down to 0 fixes this.Final Answer:
Change the last for loop to iterate from n-1 down to 0 for stable sorting -> Option AQuick Check:
Stable counting sort = reverse iteration [OK]
- Iterating forward breaks stable sorting
- Misunderstanding count array initialization
- Ignoring cumulative sum step
{"002", "010", "001", "100"}, how can you adapt Radix Sort to correctly sort them as numbers?Solution
Step 1: Understand sorting numeric strings with leading zeros
Sorting strings lexicographically will not sort numbers correctly if leading zeros exist.Step 2: Convert strings to integers for numeric sorting
Convert strings to integers to remove leading zeros, then apply Radix Sort on integers for correct numeric order.Final Answer:
Convert strings to integers first, then apply Radix Sort on integers -> Option DQuick Check:
Numeric string sorting = convert to int then Radix Sort [OK]
- Sorting strings lexicographically causes wrong order
- Ignoring leading zeros impact
- Using only first character for sorting
Ready to prove it?
Pass the test and this pattern moves unknown
