The time complexity of counting sort
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Counting sort is an efficient sorting algorithm for dealing with a collection of items that have keys lying in a well-defined range. Unlike comparison-based algorithms, counting sort's performance is not limited by the number of elements to be sorted, but rather by the range of the keys. It is an integer sorting algorithm that is particularly notable for its linear time complexity under certain conditions.
Overview of Counting Sort
Counting sort operates on an array of integer keys, transforming them into a position-based index map to organize the items directly. It requires extra space for counting the occurrences of each distinct key. The main idea is to count the occurrence of each key, aggregate the counts to determine the position of each key in the output sequence, and finally, compile the sorted array.
Steps of Counting Sort:
- Count the Occurrences: Create a count array that holds the number of times each key appears. The size of this array should be equal to the range of the keys.
- Calculate Cumulative Counts: Transform the count array into a cumulative count array by iterating through and adding the count of each key to the count of the previous key.
- Place Elements in Output: Using the cumulative count array, place each object from the input list into its correct position in the output array. Decrease the count in the cumulative array as items are placed.
- Copy Back to Original Array: If needed, the sorted array can be copied back into the original array.
Example:
Consider an array of integers `[4, 2, 2, 8, 3, 3, 1]`.
• Initial Count Array: Initialize an array `count` of size `max_value + 1` (where `max_value` is the largest number in the input array), all elements set to zero.
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
• Count Occurrences: Update the count array with the frequency of each element.
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 0 | 1 | 2 | 2 | 1 | 0 | 0 | 0 | 1 |
• Calculate Cumulative Counts:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 0 | 1 | 3 | 5 | 6 | 6 | 6 | 6 | 7 |
• Place Elements in Output Array:
| Output |
| [1, 2, 2, 3, 3, 4, 8] |
Time Complexity of Counting Sort
Complexity Analysis
The time complexity of counting sort depends on the size of the input array (`n`) and the range of the input (`k`).
- Counting Occurrences: This step runs in time since each element from the input array needs to be examined and counted once.
- Cumulative Counts: This involves a single pass through the `count` array, taking time.
- Building the Output: This takes time, as you are placing each element in the output array based on the indices in your cumulative count array.
Combining these parts, the time complexity of counting sort is .
Practical Considerations
• The algorithm is optimal when the range of the numbers (k) is not significantly larger than the number of elements (n). Specifically, counting sort performs efficiently when , leading to a linear time complexity, i.e., . • The space complexity of counting sort is , due to the `count` array and the output array.
Key Points Summary
| Parameter | Description |
| Input Requirements | Works with non-negative integers or numbers with known range. |
| Time Complexity | where n is the number of elements and k is the range of input. |
| Space Complexity | |
| Stable Sort | Yes |
| Parallelizable | Yes |
| Best Use Case | Sorting integers with known small range |
Conclusion
Counting sort is a highly efficient, stable, and non-comparison sorting algorithm when applied under appropriate conditions (i.e., small range relative to input size). It is especially suited for applications involving integers or discrete range data, such as sorting scores in exams, organizing frequency data, or multi-key sorting in conjunction with other sorting algorithms. Counting sort manages to sidestep the traditional lower bound of comparison sorts, achieving linear time sorting through direct use of input keys to steer the sorting process.
Related reading
- Three Way Merge Algorithms for Text
- Tickmark algorithm for a graph axis
- Tie breaking in a priority queue using python
- Time complexity analysis for finding the maximum element
- Theoretically can the Ackermann function be optimized?
- This TensorFlow binary is optimized with IntelR MKL-DNN to use the following CPU instructions in performance critical
- Time complexity deleting element of deque
- Time complexity for a very complicated recursion code

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.