Counting Sort is a non-comparison-based sorting algorithm that works well when there is a limited range of input values. It is particularly efficient when the range of input values is small compared to the number of elements to be sorted. The basic idea behind Counting Sort is to count the frequency of each distinct element in the input array and use that information to place the elements in their correct sorted positions.
Algorithm:
- Find the range of the input array (minimum and maximum elements).
- Create a count array to store the count of each unique object.
- Store the count of each unique object in the count array.
- Modify the count array by storing the actual position of each object in the output array.
- Build the output array using the modified count array and the original input array.
- Copy the sorted elements into the original array.