Counting Sort Algorithm in DSA Python - Time & Space Complexity
Counting Sort is a special sorting method that works well when numbers are within a small range.
We want to understand how the time it takes grows as the list size and number range grow.
Analyze the time complexity of the following code snippet.
def counting_sort(arr):
max_val = max(arr)
count = [0] * (max_val + 1)
for num in arr:
count[num] += 1
sorted_arr = []
for i in range(len(count)):
sorted_arr.extend([i] * count[i])
return sorted_arr
This code sorts numbers by counting how many times each number appears, then rebuilding the sorted list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two loops: one to count each number, one to build the sorted list.
- How many times: First loop runs once for each element in input (n times). Second loop runs once for each number from 0 to max value (k times).
Execution grows based on both the number of elements and the range of numbers.
| Input Size (n) | Range Size (k) | Approx. Operations |
|---|---|---|
| 10 | 5 | About 10 + 5 = 15 |
| 100 | 50 | About 100 + 50 = 150 |
| 1000 | 100 | About 1000 + 100 = 1100 |
Pattern observation: The time grows roughly by adding the input size and the range size.
Time Complexity: O(n + k)
This means the time depends on both how many numbers you sort and the size of the number range.
[X] Wrong: "Counting Sort always runs in linear time O(n)."
[OK] Correct: The time also depends on the range of numbers (k). If k is very large, the algorithm slows down.
Understanding Counting Sort's time complexity helps you choose the right sorting method based on data size and range, a useful skill in many coding challenges.
"What if the input numbers are very large but sparse? How would that affect the time complexity of Counting Sort?"
Practice
Counting Sort algorithm?Solution
Step 1: Understand Counting Sort principle
Counting Sort counts how many times each value appears in the list.Step 2: Use counts to place elements
It then uses these counts to place elements directly in the sorted order without comparisons.Final Answer:
Counting the occurrences of each element and using that to place elements in order -> Option DQuick Check:
Counting occurrences = A [OK]
- Thinking Counting Sort compares elements
- Confusing Counting Sort with comparison-based sorts
- Assuming Counting Sort works on all data types
Solution
Step 1: Determine count array size
For values 0 to 5 inclusive, we need 6 slots (0,1,2,3,4,5).Step 2: Initialize count array with zeros
Using[0] * 6creates a list of six zeros to count occurrences.Final Answer:
count = [0] * 6 -> Option AQuick Check:
Length 6 with zeros = D [OK]
- Using wrong size for count array
- Initializing with null instead of 0
- Leaving count array empty
[4, 2, 2, 8, 3, 3, 1]?
def counting_sort(arr):
max_val = max(arr)
count = [0] * (max_val + 1)
for num in arr:
count[num] += 1
sorted_arr = []
for i in range(len(count)):
sorted_arr.extend([i] * count[i])
return sorted_arr
print(counting_sort([4, 2, 2, 8, 3, 3, 1]))Solution
Step 1: Count occurrences of each number
Count array for max_val=8: counts for 1=1, 2=2, 3=2, 4=1, 8=1, others=0.Step 2: Build sorted list using counts
Extend sorted_arr with each number repeated by its count: [1, 2, 2, 3, 3, 4, 8].Final Answer:
[1, 2, 2, 3, 3, 4, 8] -> Option AQuick Check:
Sorted list matches counting sort output = B [OK]
- Forgetting to repeat elements by their count
- Returning unsorted or reversed list
- Including zeros for missing elements
def counting_sort_bug(arr):
max_val = max(arr)
count = [0] * max_val
for num in arr:
count[num] += 1
sorted_arr = []
for i in range(len(count)):
sorted_arr.extend([i] * count[i])
return sorted_arr
print(counting_sort_bug([3, 1, 2, 3]))Solution
Step 1: Check count array size
Count array size ismax_val, but should bemax_val + 1to include max element index.Step 2: Understand impact on counting
Missing last index causes error when counting max element (3), leading to IndexError or wrong counts.Final Answer:
Count array size is too small, missing last element count -> Option CQuick Check:
Count array size = max_val + 1 [OK]
- Using max_val instead of max_val + 1 for count array size
- Ignoring IndexError from out-of-range access
- Not initializing count array properly
Solution
Step 1: Apply Counting Sort on scores 0-100
Create count array of size 101, count occurrences of each score.Step 2: Use count array to find number of 75 scores
Count at index 75 directly gives number of students scoring 75 without scanning sorted list.Final Answer:
Use Counting Sort normally, then check count[75] for number of students scoring 75 -> Option BQuick Check:
Counting array index = score count, access directly [OK]
- Scanning sorted list instead of using count array
- Ignoring missing scores in count array
- Using slower comparison sorts unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
