What if you could sort thousands of numbers instantly without comparing each one?
Why Counting Sort Algorithm in DSA Python?
Imagine you have a big box of colored balls, and you want to arrange them by color quickly. If you try to pick each ball and compare it with every other ball to find its place, it will take a long time and be very tiring.
Sorting by comparing each item one by one is slow and confusing, especially when there are many items. It's easy to make mistakes and waste time, like mixing up colors or missing some balls.
Counting Sort helps by counting how many balls of each color there are first. Then, it uses these counts to place each ball directly in the right spot, making sorting very fast and simple.
def sort_numbers(numbers): for i in range(len(numbers)): for j in range(i + 1, len(numbers)): if numbers[i] > numbers[j]: numbers[i], numbers[j] = numbers[j], numbers[i]
def counting_sort(numbers): max_value = max(numbers) count = [0] * (max_value + 1) for number in numbers: count[number] += 1 sorted_numbers = [] for value, freq in enumerate(count): sorted_numbers.extend([value] * freq) return sorted_numbers
Counting Sort makes it possible to sort large lists of numbers very quickly when the numbers are within a known range.
Counting Sort is like sorting exam scores where you know the scores range from 0 to 100. You count how many students got each score, then list the scores in order without comparing each score to every other.
Manual sorting by comparing each item is slow and error-prone.
Counting Sort counts items first, then places them directly.
This method is fast for sorting numbers within a limited range.
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
