Colors: Orange = Counting, Blue = Current, Purple = Placing, Yellow = Count Array, Green = Sorted
Counting Sort is a non-comparison sorting algorithm that sorts elements by counting occurrences of each distinct element.
How it works:
Time Complexity: O(n + k) where k is range of input
Scenario: A school needs to sort 500 student test scores (range 0-100) and generate a frequency distribution to analyze class performance across score brackets.
Input: 500 test scores between 0-100 (e.g., [78, 92, 65, 88, 95, 72, 78, 85, 88, 92...])
Process: Create count array for each score 0-100, count occurrences, output sorted scores maintaining stability
Output: Sorted scores [65, 68, 72, 74, 77, 78, 78, 81, 85, 87, 88, 88, 89, 90, 91, 92, 92, 95...] with frequency histogram
Counting sort excels here because test scores are bounded integers (0-100), and processing is O(n + k) = O(500 + 100) versus comparison sort's O(n log n) = O(500 * log 500). The count array simultaneously provides the sorted output and a frequency histogram showing how many students scored in each range. This is stable sorting, so students with identical scores maintain their original order, and the algorithm works directly in linear time without recursion overhead.
Benefits: Linear time O(n+k), stable sort, frequency distribution included, ideal for bounded integer ranges