Counting sort

Counting sort is not based on comparisons like most other sorting methods are, and its time complexity is thus not bounded by O(nlogn) as all comparison sorts are. This method of sorting is used when all elements to be sorted fall in a known, finite and reasonably small range.

For example, we might need to sort a billion numbers which all lie between 1 and 1000. In this case, the best comparison sort would take O(nlogn) time, which is rather slow. However, we can create an array with 1000 elements. As we read in each number, we increment its corresponding array element by 1. When all inputs have been read in, we iterate through the 1000 elements in the array and we can print the numbers in order.

In the following pseudocode, there are N input numbers which all lie between 0 and (K-1), inclusive.

for i in 0 to (K - 1) counts[i] = 0

for each input number n   counts[n] = counts[n] + 1

for i in 0 to (K - 1) for j in 0 to counts[i] output i

The time complexity goes as follows. It takes $$O(K)$$ time to inititialize the array, $$O(N)$$ time to read in the numbers and increment the appropriate element of counts, and another $$O(N)$$ time to output the sorted list, for a total runtime of $$O(N + K)$$.