Counting Sort Algorithm - The Coding Shala

Last Updated: 20-Jan-2021
Home >> Algorithms >> Counting Sort

In this post, we will learn what is Counting Sort Algorithm and how it works. We will write a Java program for Counting Sort.

Counting Sort Algorithm

Counting Sort Algorithm is an algorithm used to sort a given array. We use counting sort if given array elements are in a specific range. It works by counting the number of objects having distinct key values and using those counts we compute an item's index in the final sorted array.

Working of Counting Sort Algorithm

The following steps we follow for the counting sort algorithm:
• Step 1. We will iterate through the input once and count the number of times each item occurs.
• Step 2. Now we modify our count array to pre-compute where each item from the input should go. This step is a little be tricky. We add the previous sum to the current element in the count array. To get more understanding see the below example.
• Step 3. Now we will use our pre-computed count array to put all the elements at the given sum index in the count array and then decrease the value in the count array by 1.
Here is an example of a counting sort algorithm:

We have an array  -> {4,8,4,2,9,9,6,2,9}
In the above-given array, the range of numbers is 0 to 10(inclusive).

The idea is to count how many 0's we see in an array, how many 1's, and so on. There are 11 possible values in the range of 0 to 10, we will use an array with 11 counters initialized to 0.

counts array:
0   0   0   0   0    0   0   0    0   0   0
|     |    |    |     |     |    |    |     |    |    |
0   1   2   3   4    5   6   7    8   9  10

Now we iterate the input array once and store the count at index.

INPUT->  4 8 4 2 9 9 6 2 9

The first item 4 comes we add one to counts[4] and like this for the rest of the others.
Final Count array:
0   0    2   0   2    0   1   0   1   3   0
|     |     |    |    |     |     |    |    |    |     |
0    1    2   3   4    5   6   7   8   9  10

Now we will build the index array means will modify the counts array so can decide where each item from the input should go.

We modify the counts' array such that each element at each index stores the sum of previous counts.

Counts array ->                     0 0 2 0 2 0 1 0 1 3 0
Counts array after modify ->   0 0 2 2 4 4 5 5 6 9 9

Now for the final step, we insert items in the output array by taking index from the counts' array.

Input Array->     4 8 4 2 9 9 6 2 9
Counts array->   0 0 2 2 4 4 5 5 6 9 9

Output array: the first item is 4 so look the index 4 in the counts' array is 4 so we will insert the 4 in the output array at 4-1(3) index.

Output array->  0 0 0 4 0 0 0 0 0

And will decrease the counts[4] by 1.   counts-> 0 0 2 2 3 4 5 5 6 9 9

The next element is 8 will go counts[8] = 9 so will add 8 at 9-1=8 index in output and so on.

Final output array is -> 2 2 4 4 6 8 9 9 9

The time complexity of Counting sort

Counting sort algorithm takes O(n+k) time in average, best, and worst case. The counting sort algorithm takes O(n+k) Space, where n is the length of the given array and k is the range of numbers.

So we can say counting sort takes O(n) time and space. So why don't we use it everywhere? The reason is Counting sort only works when the range of items in the input is known ahead of time. And the range of values is big then for counting sort requires a lot of space.

Counting Sort Algorithm Java Program

Java Program:

```//counting sort
//the coding shala

class Main{

public static void counting_sort(int[] input) {
int len = input.length;
//range is 0 to 10(inclusive)
int[] output = new int[len];
int[] counts = new int[11];

//step 1 count the numbers of element
for(int i=0;i<len;i++) {
counts[input[i]]++;
}

//modify the count array to get index to sort the array
for(int i=1; i<counts.length; i++) {
counts[i] += counts[i-1];
}

//now take the index from counts array
//insert at index in output array
//decrease the value by 1 in counts array
for(int i=0; i<len; i++) {
int ele = input[i];
int index = counts[ele];
output[index-1] = ele;
counts[ele]--;
}

//insert back to original array
for(int i=0; i<len;i++) {
input[i] = output[i];
}
}

public static void main(String[] args) {
int[] input = {4,8,4,2,9,9,6,2,9};
int len = input.length;
counting_sort(input);
for(int i=0;i<len;i++) {
System.out.print(input[i]+" ");
}
}
}
```

Other Posts You May Like