0% found this document useful (0 votes)
97 views7 pages

Counting Sort Algorithm Explained

Counting sort is a sorting algorithm that works by counting the number of objects that have distinct key values and using arithmetic to determine the position of each object in the sorted output array. It runs in O(n+k) time where n is the number of items and k is the range of possible key values. It requires an auxiliary array of size k to store the count of each key value. Counting sort can only be used to sort integers when the possible values are known and are a small number compared to the list size.

Uploaded by

Badnaam Raja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views7 pages

Counting Sort Algorithm Explained

Counting sort is a sorting algorithm that works by counting the number of objects that have distinct key values and using arithmetic to determine the position of each object in the sorted output array. It runs in O(n+k) time where n is the number of items and k is the range of possible key values. It requires an auxiliary array of size k to store the count of each key value. Counting sort can only be used to sort integers when the possible values are known and are a small number compared to the list size.

Uploaded by

Badnaam Raja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Counting Sort

• Sorting according to the key value


• No comparison
• Only for positive numbers
• Values should be in the range of k value
• Counting the elements having different key
values
Algorithm:
Let's assume that, array A of size N needs to be sorted.
•Initialize the auxillary array Aux[] as 0.

Note: The size of this array should be ≥max(A[]).


•Traverse array A and store the count of occurrence of each element in the
appropriate index of the Aux array, which means, execute Aux[A[i]]++ for
each i, where i ranges from [0,N−1].
•Initialize the empty array sortedA[]
•Traverse array Aux and copy i into sortedA for Aux[i] number of times
where 0≤i≤max(A[]).

Note: The array A can be sorted by using this algorithm only if the maximum


value in array A is less than the maximum size of the array Aux.
Countsort()
{
Int k=0;
for(i=0;i<n;i++)
K=max(K,A[i]);
For(i=1;i<=K;i++)
Aux[i] = 0;
for(i=0;i<n;i++)
Aux[A[i]]++;
Int j=0;
For(i=0;i<=K;i++)
{
int temp = Aux[i];
while(temp--)
{
sortedA[j] = i;
j++;
}
A[4,2,7,1,2,4,9,2]
K=9 A[4,2,7,1,2,4,9,2]
Aux[0 0 0 0 0 0 0 0 0 0 ] Aux[0 1 3 0 2 0 0 1 0 1 ]
Aux[0 0 0 0 1 0 0 0 0 0 ]
Aux[0 0 1 0 1 0 0 0 0 0 ]
SortedA[1 2 2 2 4 4 7 9]
Aux[0 0 1 0 1 0 0 1 0 0 ]
Aux[0 1 1 0 1 0 0 1 0 0 ]
Aux[0 1 2 0 1 0 0 1 0 0 ]
Aux[0 1 2 0 2 0 0 1 0 0 ]
Aux[0 1 2 0 2 0 0 1 0 1 ]
Aux[0 1 3 0 2 0 0 1 0 1 ]
Complexity

The array A is traversed in O(N) time and the
resulting sorted array is also computed
in O(N) time. 
• Aux[] is traversed in O(K) time.
• Therefore, the overall time complexity of
counting sort algorithm is O(N+K).
• Drawback
– If the K value is much higher than the size of the
input array, then, it is not efficient.

You might also like