Counting Sort 1

  • + 1 comment

    Can you explain the logic of this code.

    • + 1 comment

      yes Basically, counting sort on the concept count the frequency of the elements present in the array and based on that initialize into another temporary array the array according to their frequency in increasing order or decreasing order as per our requirements. In my code I applied the given logic: 1) Firstly, I find the length of given vector that we have to sort. 2) Secondly, the maximum size of returning array given 100 that's why whole array initialize with 0s. 3) After that by using the given array updated the value in ans array for example: if element in given array is 5 then, we go to index 5 of ans array and increase value by 1 beacuse we have to find the frequency of each element to sort the array. 4) finally, return the array called ans.

      • + 0 comments

        sort(arr.begin(), arr.end()); vector ans(100,0); for (int i = 0; i <= arr[arr.size() - 1]; i++) ans[i] = (count(arr.begin(), arr.end(), i)); return ans;

        Thanks, I just took the initialization step from your solution.