COUNTINGSORT
COUNTINGSORT
COUNTINGSORT ALGORITHM.
Counting sort is a sorting algorithm that sorts the elements of an array by counting the
number of occurrences of each unique element in the array. The count is stored in an
auxiliary array and the sorting is done by mapping the count as an index of the auxiliary array.
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. Then do some arithmetic operations
to calculate the position of each object in the output sequence.
Counting sort assumes that each of the n input elements is an integer in the range 0 to k, for
some integer k. Counting sort determines, for each input element x, the number of elements less
than x. It uses this information to place element x directly into its position in the output array.
For example, if 17 elements are less than x, then x belongs in output position 18. We must
modify this scheme slightly to handle the situation in which several elements have the same
value, since we do not want to put them all in the same position.
In the code for counting sort, we assume that the input is an array A[1..n], and we require two
other auxiliary arrays: the array B[1..n] holds the sorted output, and the array C[0..k] provides
temporary working storage.
1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 1 2 3 4 5 6 7 8
A 3 6 4 1 3 4 1 4 8. C 02 2 4 7 7 8 B 4
7. C 02 2 4 6 7 8 B 1 4
6. C 01 2 4 6 7 8 B 1 4 4
0 1 2 3 4 5 6 5. C 0 1 2 4 5 7 8 B 1 3 4 4
C 0 2 0 2 3 0 1 4. C 0 1 2 3 5 7 8 B 1 1 3 4 4
3. C 0 0 2 3 5 7 8 B 1 1 3 4 4 4
2. C 0 0 2 3 4 7 8 B 1 1 3 4 4 4 6
1. C 0 0 2 3 4 7 7 B 1 1 3 3 4 4 4 6
0. C 0 0 2 2 4 7 7
Fig. 1 The operation of COUNTING-SORT on an input array A[1..8], where each element
of A is a nonnegative integer no larger than k = 6.
After the for loop of lines 1–2 initializes the array C to all zeros, the for loop of lines 3–4
inspects each input element. If the value of an input element is i , we increment C[i]. Thus, after
line 4, C[i] holds the number of input elements equal to i for each integer i = 0, 1, 2,…, k. Lines
6–7 determine for each i = 0, 1, 2,…, k how many input elements are less than or equal to i by
keeping a running sum of the array C.
Finally, the for loop of lines 9–11 places each element A[j] into its correct sorted position in the
output array B. If all n elements are distinct, then when we first enter line 9, for each A[j], the
value C[A[j]] is the correct final position of A[j] in the output array, since there are C[A[j]]
elements less than or equal A[j].
Because the elements might not be distinct, we decrement C[A[j]] each time we place a value
A[j] into the B array. Decrementing C[A[j]] causes the next input element with a value equal to
A[j], if one exists, to go to the position immediately before A[j] in the output array.
Time Complexity
o Best Case Complexity - It occurs when there is no sorting required, i.e. the array is
already sorted. The best-case time complexity of counting sort is O(n + k).
o Average Case Complexity - It occurs when the array elements are in jumbled order that
is not properly ascending and not properly descending. The average case time complexity
of counting sort is O(n + k).
o Worst Case Complexity - It occurs when the array elements are required to be sorted in
reverse order. That means suppose you have to sort the array elements in ascending
order, but its elements are in descending order. The worst-case time complexity of
counting sort is O(n + k).
In all above cases, the time complexity of counting sort is same. This is because the algorithm
goes through n+k times, regardless of how the elements are placed in the array.
Counting sort is better than the comparison-based sorting techniques because there is no
comparison between elements in counting sort. But, when the integers are very large the
counting sort is bad because arrays of that size have to be created.