Algorithm
Counting Sort Algorithm:
- Start with an unsorted array of integers within a known range.
- Create a count array to store the frequency of each distinct integer:
- Iterate through the original array to populate the count array.
- Each index in the count array represents an integer and its frequency.
- Transform the count array to store cumulative counts:
- Cumulative counts represent the position of integers in the sorted array.
- Build the sorted array:
- Iterate through the original array in reverse order for stability.
- Place each integer at its correct position using the cumulative counts.
- Decrease the count in the count array after placing each integer.
- Replace the original array with the sorted array.
Example:

Fig. 1: Iteration by Iteration Visualization of Counting Sort
Note: Counting Sort is non-comparison-based and works best for integers within a small range.