We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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.
Counting Sort 2
You are viewing a single comment's thread. Return to all comments →
This is the simplest algorithm I used, sharing 'cause I see a lot of twisted answers here in Discussions
(C++)
while(result[i]--) can you clarify please I didnt get it
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.Thank you so much. I really appreciate your answer
Thanks, This helped me a lot.