Counting sort uses some assumptions:
- Assume the input array contains non-negative integers.
- Assume that each element is in the range 0 to , for some integer .
Example
Suppose . This implies that , where the most efficient choice of is 4.
We work with 3 arrays:
- An input array A
- An output array B
- A temporary array C
We initialize to be of length . We begin the algorithm by counting the number of elements for all . We assign each to be the number of times that appears in .