Counting Sort Algorithm

Yasir
2 min readMay 13, 2020

--

Counting Sort Algorithm in C++ and some diagrams .

In Counting sort, the frequencies of distinct elements of the array to be sorted is counted and stored in an auxiliary array, by mapping its value as an index of the auxiliary array.

Algorithm Steps for Counting Sort

In Order to Understand this algorithm in brief , let’s see some screenshots

Now lets see the code part for this algorithm

First Part of Program (Fig 1)

Line 32 (Fig 1) :- Here we are applying the main function , declaring size as zero and setting range as 10 . As range is important part for this algorithm . Then we are asking to user about size of the array and taking the elements in the array by user . And printing the element without sorting and then we are calling the CountingSort function .

Now let’s see the CountingSort Function

Second Part of Program (Fig 2)

Line 4 (Fig 2) :- Here we are making the counting sort function by passing the array and its size and range . And then making two auxilary arrays as output and count arrays.

Line 14 (Fig 2) :- Here we are using for loop starting from zero and making it till the last of array and when you will see the algorithm explanation for this program then you can only understand the purpose of what happening in this for loop

And similar for all next codes . Recommend you to watch this Video for BEST algorithm explanation . make sure watch till end without any break .

--

--

Yasir

Exploring the Swift, SwiftUI, and Apple universe.