Counting sort is a stable sorting algorithm. Counting sort uses an additional array Count_arr, where the i-th element is the number of elements whose value is equal to i in the array Arr to be sorted. Then arrange the elements in Arr to the correct position according to the array Count_arr.
is divided into four steps:
1. Find the largest and smallest elements in the array to be sorted
2. Count the number of occurrences of each element with value i in the array and store it in the array Count_arr Item i
3. Accumulate all counts (starting from the first element in Count_arr, add each item to the previous item)
4. Traverse the original array in reverse: add each element i Place it in the Count_arr(i)th item of the new array, and subtract Count_arr(i) by 1 for each element
Example: