Javatpoint Logo
Javatpoint Logo

Counting sort in Java

Counting sort is one of the most used sorting techniques in Java that is based on the keys b/w specific range. Counting sort doesn't perform sorting by comparing elements. It performs sorting by counting objects having distinct key values like hashing. After that, it performs some arithmetic operations for calculating the index position of each object in the output sequence. The counting sort algorithm is not used as a general-purpose sorting algorithm.

Suppose, we have an input array a{4, 3, 2, 5, 4, 3, 5, 1, 0, 2, 5} which have values in the range[0, 5].

Counting sort in Java

We count the occurrence of each element in the array and represent the countings as an array countArray. The countArray[i] represents the occurrence of the number i in the array a.

Counting sort in Java

So, number 0 and 1 appear one time, 2, 3, and 4 appears two times and 5 appear three times.

Counting sort in Java

Now, with the help of the countArray, we determine how many elements are less than or equal to each element of the countArray in array a. We sum all the elements which are less than or equal to countArray[i].

In array a,

  1. There is only one element, i.e., 0, which is less than or equal to zero and that is equal to countArray[0].
  2. There are two elements which are less than or equal to 1, i.e., 0 and 1 which is equal to countArray[0] + countArray[1].
  3. There are 4 values which are less than or equal to 2, i.e., 0, 1, 2, and 2 which is equal to countArray[0]+ countArray[1]+ countArray[2].
  4. There are 6 values which are less than or equal to 3, i.e., 0, 1, 2, 2, 3, 3 which is equal to countArray[0]+ countArray[1]+ countArray[2] ]+ countArray[3].
  5. There are 6 values which are less than or equal to 3, i.e., 0, 1, 2, 2, 3, 3 which is equal to countArray[0]+ countArray[1]+ countArray[2] ]+ countArray[3].
  6. There are 6 values which are less than or equal to 4, i.e., 0, 1, 2, 2, 3, 3, 4, 4 which is equal to countArray[0]+ countArray[1]+ countArray[2] ]+ countArray[3] + countArray[4].
  7. There are 6 values which are less than or equal to 5, i.e., 0, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5 which is equal to countArray[0]+ countArray[1]+ countArray[2] ]+ countArray[3] + countArray[4] + countArray[5].

Algorithm

After getting the auxiliary array, i.e., countArray, we perform sorting to sort the array a. These are the following steps to sort the elements of the array a.

  1. Iterate the input array 'a' in the reverse form.
  2. For each element 'i' in the input array, C[i] defines the location of the number i in the sorted array because there are countArray[i] elements less than or equal to i.
  3. At the end of each round, we decrement the countArray[i].

So, in order to sort the input array a, we iterate it in reverse order and start with the number 5. At index 5 in the countArray, we have value 11, which means array 'a' contains 11 elements that are less or equal to the number 5. It means 5 will be our last element in the sorted array.

Counting sort in Java

After moving number 5 in the sorted array, we decrement the value of countArray[5]. Now, the next number in the reverse order is 2, and at index 2, we have value 4, which means array 'a' contains four elements that are less than or equal to 2. So, the number will be at position 4 in the sorted array.

Counting sort in Java

The next number is 0 in the input array 'a' and the value in the countArray at index 0 is 1 which means there is only one element in the input array 'a', which is equal to 0. So, 0 will be the first element in the sorted array.

Counting sort in Java

We perform the same operation for the remaining elements of the input array 'a'.

For element 1

Counting sort in Java

For element 5

Counting sort in Java

For element 3

Counting sort in Java

For element 4

Counting sort in Java

For element 5

Counting sort in Java

For element 2

Counting sort in Java

For element 3

Counting sort in Java

For element 4

Counting sort in Java

Sorted Array

Counting sort in Java

Let's implement the logic of the counting sort in Java by using the above-discussed algorithm:

CountingSortExample.java

Output:

Counting sort in Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA