Counting Sort Algorithm in DSA Java - Time & Space Complexity
We want to understand how the time needed by Counting Sort changes as the input size grows.
Specifically, how the number of steps depends on the number of items and the range of values.
Analyze the time complexity of the following Counting Sort code.
public void countingSort(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) max = arr[i];
}
int[] count = new int[max + 1];
for (int num : arr) {
count[num]++;
}
int index = 0;
for (int i = 0; i < count.length; i++) {
while (count[i]-- > 0) {
arr[index++] = i;
}
}
}
This code sorts an array of integers by counting how many times each value appears, then rebuilding the sorted array.
Look for loops and repeated steps in the code.
- Primary operation: Three loops: one to find max, one to count frequencies, one to rebuild sorted array.
- How many times: Each loop runs proportional to input size or range size.
The time depends on both the number of items (n) and the range of values (k).
| Input Size (n) | Range Size (k) | Approx. Operations |
|---|---|---|
| 10 | 5 | About 10 + 10 + 5 = 25 |
| 100 | 50 | About 100 + 100 + 50 = 250 |
| 1000 | 200 | About 1000 + 1000 + 200 = 2200 |
Pattern observation: The total steps grow roughly as the sum of input size and range size.
Time Complexity: O(n + k)
This means the time grows linearly with both the number of items and the range of values.
[X] Wrong: "Counting Sort always runs in linear time regardless of input values."
[OK] Correct: The time also depends on the range of values (k). If k is very large, the algorithm slows down.
Understanding Counting Sort's time complexity helps you explain when it is efficient and when it is not, a useful skill in interviews.
"What if the input array contains negative numbers? How would the time complexity change?"
Practice
Counting Sort algorithm?Solution
Step 1: Understand Counting Sort principle
Counting Sort counts how many times each value appears in the input.Step 2: Use counts to place elements
These counts help to place elements directly in the sorted output without comparisons.Final Answer:
Count the occurrences of each element and use counts to place elements in order -> Option AQuick Check:
Counting occurrences = main idea [OK]
- Thinking Counting Sort compares elements
- Confusing Counting Sort with Quick Sort or Merge Sort
- Assuming Counting Sort works well with all data types
Solution
Step 1: Understand Java array declaration syntax
In Java, arrays are declared with type[], name = new type[size];Step 2: Check each option for correct syntax
int[] count = new int[10]; uses correct syntax: int[] count = new int[10]; int count[] = new int(10); uses parentheses instead of brackets, invalid. int count = new int[10]; misses brackets on left side. int[] count = new int(); misses size in brackets.Final Answer:
int[] count = new int[10]; -> Option AQuick Check:
Correct Java array syntax = int[] count = new int[10]; [OK]
- Using parentheses instead of square brackets
- Not specifying array size when creating
- Incorrect placement of brackets in declaration
int[] arr = {4, 2, 2, 8, 3, 3, 1};
int max = 8;
int[] count = new int[max + 1];
for (int num : arr) {
count[num]++;
}
int index = 0;
for (int i = 0; i <= max; i++) {
while (count[i] > 0) {
arr[index++] = i;
count[i]--;
}
}
System.out.println(java.util.Arrays.toString(arr));Solution
Step 1: Count occurrences of each number
Count array after loop: count[1]=1, count[2]=2, count[3]=2, count[4]=1, count[8]=1, others=0.Step 2: Rebuild sorted array using counts
Fill arr with 1 once, 2 twice, 3 twice, 4 once, 8 once in order.Final Answer:
[1, 2, 2, 3, 3, 4, 8] -> Option DQuick Check:
Sorted array matches [1, 2, 2, 3, 3, 4, 8] [OK]
- Not initializing count array size correctly
- Forgetting to decrement count during placement
- Printing original array without sorting
int[] arr = {3, 1, 2};
int max = 3;
int[] count = new int[max];
for (int num : arr) {
count[num]++;
}Solution
Step 1: Check count array size for indexing
Array 'count' size is max=3, so indices 0,1,2 are valid but input has value 3 which is out of bounds.Step 2: Fix count array size to include max index
Size should be max+1=4 to allow index 3 access safely.Final Answer:
Change 'int[] count = new int[max];' to 'int[] count = new int[max + 1];' to include max index -> Option BQuick Check:
Array size must cover max value index [OK]
- Using count array size equal to max value only
- Adjusting indices incorrectly instead of size
- Ignoring ArrayIndexOutOfBoundsException
arr = {5, 3, 0, 2, 3, 0, 1}, how would you modify the Counting Sort algorithm to sort it if the input can contain negative numbers as well? Choose the best approach.Solution
Step 1: Understand Counting Sort limitation with negatives
Counting Sort uses array indices as counts, so negative indices are invalid.Step 2: Shift values to non-negative range
Find minimum value, add its absolute value to all elements to make them non-negative, then apply Counting Sort.Final Answer:
Shift all numbers by adding the absolute value of the minimum negative number before counting -> Option CQuick Check:
Shift negatives to positive range for Counting Sort [OK]
- Trying to use negative indices in count array
- Ignoring negative values in input
- Using incorrect data structures without reason
Ready to prove it?
Pass the test and this pattern moves unknown
