Radix Sort Algorithm in DSA Java - Time & Space Complexity
We want to understand how the time taken by Radix Sort changes as the list size grows.
Specifically, how the number of steps grows when sorting more numbers.
Analyze the time complexity of the following code snippet.
public void radixSort(int[] arr) {
int max = Arrays.stream(arr).max().getAsInt();
for (int exp = 1; max / exp > 0; exp *= 10) {
countingSortByDigit(arr, exp);
}
}
private void countingSortByDigit(int[] arr, int exp) {
int n = arr.length;
int[] output = new int[n];
int[] count = new int[10];
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]--;
}
System.arraycopy(output, 0, arr, 0, n);
}
This code sorts numbers by processing each digit from least to most significant using counting sort.
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: The counting sort inside runs for every digit, processing all elements each time.
As the number of elements grows, the algorithm processes each element for every digit in the largest number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 to 30 steps (depends on digit count) |
| 100 | About 200 to 300 steps |
| 1000 | About 2000 to 3000 steps |
Pattern observation: Operations grow roughly in direct proportion to the number of elements and the number of digits.
Time Complexity: O(d * (n + k))
This means the time grows with the number of elements (n), the number of digits (d), and the digit range (k, here 10).
[X] Wrong: "Radix Sort is always faster than comparison sorts like QuickSort."
[OK] Correct: Radix Sort depends on digit count and range; for large digit counts or small arrays, comparison sorts can be faster.
Understanding Radix Sort's time complexity helps you explain when and why to use it, showing clear thinking about algorithm choices.
"What if the numbers had more digits or a larger digit range? 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) 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 pivot or divide and conquer
pos (starting from 0 for least significant digit) of a number num in Java?Solution
Step 1: Understand digit extraction logic
To get the digit at position pos, divide num by 10^pos to shift digits right, then take modulo 10 to get the last digit.Step 2: Check each option
(num / (int) Math.pow(10, pos)) % 10 correctly divides num by 10^pos and then modulo 10. Others do incorrect operations.Final Answer:
(num / (int) Math.pow(10, pos)) % 10 -> Option BQuick Check:
Digit extraction = divide then modulo [OK]
- Using modulo before division
- Multiplying instead of dividing
- Incorrect order of operations
[170, 45, 75, 90, 802] sorting by the least significant digit (units place)?Solution
Step 1: Extract least significant digit of each number
170 -> 0, 45 -> 5, 75 -> 5, 90 -> 0, 802 -> 2Step 2: Sort array by these digits
Order by digits: 0s first (170, 90), then 2 (802), then 5s (45, 75). So array becomes [170, 90, 802, 45, 75]Final Answer:
[170, 90, 802, 45, 75] -> Option CQuick Check:
Sort by units digit = [170, 90, 802, 45, 75] [OK]
- Sorting entire numbers instead of digits
- Mixing digit positions
- Ignoring stable sorting requirement
int digit = (num % (int) Math.pow(10, pos)) / (int) Math.pow(10, pos - 1);What is the error in this code?
Solution
Step 1: Analyze division when pos = 0
When pos = 0, expression divides by Math.pow(10, -1), which is 10^-1 = 0.1, but casting to int makes it 0, causing division by zero error.Step 2: Check other options
Modulo and division order is correct for digit extraction, casting is intentional, so no other errors.Final Answer:
Division by zero when pos = 0 -> Option DQuick Check:
pos=0 causes division by zero [OK]
- Ignoring pos=0 edge case
- Assuming casting fixes division issues
- Thinking modulo order is wrong
[329, 457, 657, 839, 436, 720, 355], how many passes (iterations over digits) will Radix Sort perform to fully sort this array?Solution
Step 1: Find the maximum number of digits in the array
The largest number is 839, which has 3 digits.Step 2: Determine passes needed
Radix Sort performs one pass per digit, so 3 passes are needed (units, tens, hundreds).Final Answer:
3 passes -> Option AQuick Check:
Max digits = 3, passes = 3 [OK]
- Counting array length instead of digit count
- Assuming fixed number of passes
- Ignoring leading zeros in numbers
Ready to prove it?
Pass the test and this pattern moves unknown
