Counting Sort is a type of sorting algorithm used to sort data structures with the help of counts.

Counting sort is efficient to use when:

  • values of element lie in the small range.
  • the values are non-negative.

Counting Sort Algorithm

Counting array– It is the array which stores the counts of occurrence of each value.

For example in our case:

Array values ranging from 2 to 15

  1. Count the occurrence of value in the array and store it in counting Array.
  2. Track the counting array and rewrite the original array.

C

C++

Java

Output

Overview of Counting Sort

  • Not an in-place algorithm (requires an extra array i.e counting array).
  • O(n)-complexity.
  • To make it a stable sort, some extra steps are needed.

The basic concept is to sort the array with the help of the counts. That’s all for the counting sort program. If you face any problem then comment below.

Leave a Reply

Close Menu