Counting Sort

Counting Sort is a distributive sorting algorithm that can only sort non-negative integers. Its time complexity is $$O(n+m)$$, where m is the largest number in the array. It is stable, not adaptive, and uses $$O(m)$$ memory.

Algorithm
There are two external arrays used for sorting the algorithm: one for storing the output, and the other for counting the items in each key.

The algorithm starts by passing rightward through the original array, computing a histogram with each key.

Afterwards, prefix sum is calculated on the histogram, to determine where to place the integers, by using them as indices.

The algorithm then passes leftward to the original array and placed onto the output array, with the index to place the element being the key-th element of the prefix sum. The accessed element in the prefix sum is decremented to ensure it does not leave empty values (or overwrite them) with duplicate values.

The algorithm outputs the output array.

Example
Take the array

[12 6 7 8 5 4 10 13 11 1 14 2 15 9 16 3]

First, compute a histogram:

[0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]

Apply prefix sum:

[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]

Determine positions of input values...

[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]

Sorted!

Pseudocode
procedure counting_sort(array arr) do array output = array of length(arr) 0s array histogram = array of max(arr) + 1 0s for i = 0 to length(arr) - 1 do histogram[arr[i]] ++ end for for i = 1 to max(arr) do histogram[i] += histogram[i - 1] end for for i = length(arr) - 1 to 0 do count[arr[i]] -- output[count[arr[i]]] = arr[i] end for arr = output end