Counting Sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence.

Let us understand it with the help of an example.

For simplicity, consider the data in the range 0 to 9. 
Input data: 1, 4, 1, 2, 7, 5, 2
  1) Take a count array to store the count of each unique object.
  Index:     0  1  2  3  4  5  6  7  8  9
  Count:     0  2  2  0   1  1  0  1  0  0

  2) Modify the count array such that each element at each index 
  stores the sum of previous counts. 
  Index:     0  1  2  3  4  5  6  7  8  9
  Count:     0  2  4  4  5  6  6  7  7  7

The modified count array indicates the position of each object in 
the output sequence.
 
  3) Output each object from the input sequence followed by 
  decreasing its count by 1.
  Process the input data: 1, 4, 1, 2, 7, 5, 2. Position of 1 is 2.
  Put data 1 at index 2 in output. Decrease count by 1 to place 
  next data 1 at an index 1 smaller than this index.
[ad type=”banner”]

Following is C implementation of counting sort.

[pastacode lang=”c” manual=”%2F%2F%20C%20Program%20for%20counting%20sort%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstring.h%3E%0A%23define%20RANGE%20255%0A%20%0A%2F%2F%20The%20main%20function%20that%20sort%20the%20given%20string%20arr%5B%5D%20in%0A%2F%2F%20alphabatical%20order%0Avoid%20countSort(char%20arr%5B%5D)%0A%7B%0A%20%20%20%20%2F%2F%20The%20output%20character%20array%20that%20will%20have%20sorted%20arr%0A%20%20%20%20char%20output%5Bstrlen(arr)%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20Create%20a%20count%20array%20to%20store%20count%20of%20inidividul%0A%20%20%20%20%2F%2F%20characters%20and%20initialize%20count%20array%20as%200%0A%20%20%20%20int%20count%5BRANGE%20%2B%201%5D%2C%20i%3B%0A%20%20%20%20memset(count%2C%200%2C%20sizeof(count))%3B%0A%20%0A%20%20%20%20%2F%2F%20Store%20count%20of%20each%20character%0A%20%20%20%20for(i%20%3D%200%3B%20arr%5Bi%5D%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%2B%2Bcount%5Barr%5Bi%5D%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20Change%20count%5Bi%5D%20so%20that%20count%5Bi%5D%20now%20contains%20actual%0A%20%20%20%20%2F%2F%20position%20of%20this%20character%20in%20output%20array%0A%20%20%20%20for%20(i%20%3D%201%3B%20i%20%3C%3D%20RANGE%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20count%5Bi%5D%20%2B%3D%20count%5Bi-1%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20Build%20the%20output%20character%20array%0A%20%20%20%20for%20(i%20%3D%200%3B%20arr%5Bi%5D%3B%20%2B%2Bi)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20output%5Bcount%5Barr%5Bi%5D%5D-1%5D%20%3D%20arr%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20–count%5Barr%5Bi%5D%5D%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Copy%20the%20output%20array%20to%20arr%2C%20so%20that%20arr%20now%0A%20%20%20%20%2F%2F%20contains%20sorted%20characters%0A%20%20%20%20for%20(i%20%3D%200%3B%20arr%5Bi%5D%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20arr%5Bi%5D%20%3D%20output%5Bi%5D%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20char%20arr%5B%5D%20%3D%20%22geeksforgeeks%22%3B%2F%2F%22applepp%22%3B%0A%20%0A%20%20%20%20countSort(arr)%3B%0A%20%0A%20%20%20%20printf(%22Sorted%20character%20array%20is%20%25s%5Cn%22%2C%20arr)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”c” highlight=”” provider=”manual”/]

JAVA

[pastacode lang=”java” manual=”%2F%2F%20Java%20implementation%20of%20Counting%20Sort%0Aclass%20CountingSort%0A%7B%0A%20%20%20%20void%20sort(char%20arr%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20The%20output%20character%20array%20that%20will%20have%20sorted%20arr%0A%20%20%20%20%20%20%20%20char%20output%5B%5D%20%3D%20new%20char%5Bn%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Create%20a%20count%20array%20to%20store%20count%20of%20inidividul%0A%20%20%20%20%20%20%20%20%2F%2F%20characters%20and%20initialize%20count%20array%20as%200%0A%20%20%20%20%20%20%20%20int%20count%5B%5D%20%3D%20new%20int%5B256%5D%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3C256%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20count%5Bi%5D%20%3D%200%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20store%20count%20of%20each%20character%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%2Bcount%5Barr%5Bi%5D%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Change%20count%5Bi%5D%20so%20that%20count%5Bi%5D%20now%20contains%20actual%0A%20%20%20%20%20%20%20%20%2F%2F%20position%20of%20this%20character%20in%20output%20array%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D1%3B%20i%3C%3D255%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20count%5Bi%5D%20%2B%3D%20count%5Bi-1%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Build%20the%20output%20character%20array%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%3Cn%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20output%5Bcount%5Barr%5Bi%5D%5D-1%5D%20%3D%20arr%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20–count%5Barr%5Bi%5D%5D%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Copy%20the%20output%20array%20to%20arr%2C%20so%20that%20arr%20now%0A%20%20%20%20%20%20%20%20%2F%2F%20contains%20sorted%20characters%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%3Cn%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bi%5D%20%3D%20output%5Bi%5D%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Driver%20method%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20CountingSort%20ob%20%3D%20new%20CountingSort()%3B%0A%20%20%20%20%20%20%20%20char%20arr%5B%5D%20%3D%20%7B’g’%2C%20’e’%2C%20’e’%2C%20’k’%2C%20’s’%2C%20’f’%2C%20’o’%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20’r’%2C%20’g’%2C%20’e’%2C%20’e’%2C%20’k’%2C%20’s’%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%3B%0A%20%0A%20%20%20%20%20%20%20%20ob.sort(arr)%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.print(%22Sorted%20character%20array%20is%20%22)%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Carr.length%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(arr%5Bi%5D)%3B%0A%20%20%20%20%7D%0A%7D” message=”java” highlight=”” provider=”manual”/]

PYTHON

[pastacode lang=”python” manual=”%23%20Python%20program%20for%20counting%20sort%0A%20%0A%23%20The%20main%20function%20that%20sort%20the%20given%20string%20arr%5B%5D%20in%20%0A%23%20alphabetical%20order%0Adef%20countSort(arr)%3A%0A%20%0A%20%20%20%20%23%20The%20output%20character%20array%20that%20will%20have%20sorted%20arr%0A%20%20%20%20output%20%3D%20%5B0%20for%20i%20in%20range(256)%5D%0A%20%0A%20%20%20%20%23%20Create%20a%20count%20array%20to%20store%20count%20of%20inidividul%0A%20%20%20%20%23%20characters%20and%20initialize%20count%20array%20as%200%0A%20%20%20%20count%20%3D%20%5B0%20for%20i%20in%20range(256)%5D%0A%20%0A%20%20%20%20%23%20For%20storing%20the%20resulting%20answer%20since%20the%20%0A%20%20%20%20%23%20string%20is%20immutable%0A%20%20%20%20ans%20%3D%20%5B%22%22%20for%20_%20in%20arr%5D%0A%20%0A%20%20%20%20%23%20Store%20count%20of%20each%20character%0A%20%20%20%20for%20i%20in%20arr%3A%0A%20%20%20%20%20%20%20%20count%5Bord(i)%5D%20%2B%3D%201%0A%20%0A%20%20%20%20%23%20Change%20count%5Bi%5D%20so%20that%20count%5Bi%5D%20now%20contains%20actual%0A%20%20%20%20%23%20position%20of%20this%20character%20in%20output%20array%0A%20%20%20%20for%20i%20in%20range(256)%3A%0A%20%20%20%20%20%20%20%20count%5Bi%5D%20%2B%3D%20count%5Bi-1%5D%0A%20%0A%20%20%20%20%23%20Build%20the%20output%20character%20array%0A%20%20%20%20for%20i%20in%20range(len(arr))%3A%0A%20%20%20%20%20%20%20%20output%5Bcount%5Bord(arr%5Bi%5D)%5D-1%5D%20%3D%20arr%5Bi%5D%0A%20%20%20%20%20%20%20%20count%5Bord(arr%5Bi%5D)%5D%20-%3D%201%0A%20%0A%20%20%20%20%23%20Copy%20the%20output%20array%20to%20arr%2C%20so%20that%20arr%20now%0A%20%20%20%20%23%20contains%20sorted%20characters%0A%20%20%20%20for%20i%20in%20range(len(arr))%3A%0A%20%20%20%20%20%20%20%20ans%5Bi%5D%20%3D%20output%5Bi%5D%0A%20%20%20%20return%20ans%20%0A%20%0A%23%20Driver%20program%20to%20test%20above%20function%0Aarr%20%3D%20%22geeksforgeeks%22%0Aans%20%3D%20countSort(arr)%0Aprint%20%22Sorted%20character%20array%20is%20%25s%22%20%20%25(%22%22.join(ans))%0A%20%0A” message=”python” highlight=”” provider=”manual”/]

Output:

Sorted character array is eeeefggkkorss

Time Complexity: O(n+k) where n is the number of elements in input array and k is the range of input.
Auxiliary Space: O(n+k)

Points to be noted:
1. Counting sort is efficient if the range of input data is not significantly greater than the number of objects to be sorted. Consider the situation where the input sequence is between range 1 to 10K and the data is 10, 5, 10K, 5K.
2. It is not a comparison based sorting. It running time complexity is O(n) with space proportional to the range of data.
3. It is often used as a sub-routine to another sorting algorithm like radix sort.
4. Counting sort uses a partial hashing to count the occurrence of the data object in O(1).
5. Counting sort can be extended to work for negative inputs also.

Exercise:
1. Modify above code to sort the input data in the range from M to N.
2. Modify above code to sort negative input data.
3. Is counting sort stable and online?
4. Thoughts on parallelizing the counting sort algorithm.

[ad type=”banner”]