A stable and linear-time sorting algorithm for items with keys within a limited size of collection. [Time: O(n+k), Space: O(n+k)]
Idea
The idea of counting sort is to count the numbers of items according to each distinct key, then decide each item’s position using prefix sum. The time complexity for counting sort is O(n+k) where n is the number of items and k is the number of possible keys for each item.
Implementation
Below is an implementation of counting sort in golang.
package sorting |
References: