Counting sort
Counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key value, and using arithmetic on those counts to determine the positions of each key value in the output sequence. Its running time is linear in the number of items and the difference between the maximum and minimum key values, so it is only suitable for direct use in situations where the variation in keys is not significantly greater than the number of items.
Performance
Complexity | |
---|---|
Worst-case performance | O(n + k) |
Best-case performance | O(n + k) |
Average performance | O(n + k) |
Worst-case space complexity | O(n + k) |
Where n is the number of elements in input array and k is the range of input
Implementations
Language | Link | |
---|---|---|
Python | counting_sort.py | |
C++ | counting_sort.py |