Counting Sort 2

  • + 2 comments

    In result vector, the frequency of the elements is stored, so suppose if "result[0] = 4" , this would mean there are 4 zeroes in our given array. Hence we would just push "0" 4 times in our sorted array (our final answer.) And to that - while(result[0]--) , this while loop will run until that 4 becomes 0, hence the "0" will get push_backed 4 times in "sorted" array.