Counting Sort is a non-comparison-based sorting algorithm. It is particularly efficient when the range of input values is small compared to the number of elements to be sorted.
- The basic idea behind Counting Sort is to count the frequency of each distinct element in the input array and use that information to place the elements in their correct sorted positions.
- It works well when the range of input elements is small and comparable to the size of the array. For example, for input [1, 4, 0, 2, 1, 1], the size of array is 6 and range of elements is from 0 to 4
- If range of input array is of order more than n Log n where n is size of the array, then we can better sort the array using a standard comparison based sorting algorithm like Merge Sort.
Counting Sort Algorithm
- Declare a count array cntArr[] of size max(arr[])+1 and initialize it with 0.
- Traverse input array arr[] and map each element of arr[] as an index of cntArr[] array, i.e., execute cntArr[arr[i]]++ for 0 <= i < N.
- Calculate the prefix sum at every index of cntArr[].
- Create an array ans[] of size N.
- Traverse array arr[] from end and update ans[ cntArr[ arr[i] ] - 1] = arr[i]. Also, update cntArr[ arr[i] ] = cntArr[ arr[i] ]- - .
Why do we compute Prefix Sums?
We could simply count occurrences of all elements and one by one put them in the output array, but we computer prefix sums to achieve stability in the algorithm. Note that, after building the prefix sum cntArr[], we traverse the array from right end to ensure that the last occurrence moves to the last correct position in the sorted array.
Working of Counting Sort