55,010 views 309 on YTPak
474 20

Published on 29 Oct 2014 | over 2 years ago

EDIT: in B at index 8, the number should be 5. I mistakenly put 9 (which was the accumulative occurrence of 5). Thanks to @Rafal Krzychowiec.

A complete example of how counting sort algorithm actually works, and how it is related to its pseudo code.
Counting Sort is a stable sort and it can be used as a sub-routine for Radix Sort.
In case your range of numbers in the input array A does not start from zero (let's say starts from the number x), you can easily create another array A1, where
A1[j] = A[j] - x;
and then sort A1 instead of A. This way, the numbers in A1 will be the same numbers of A with a deduction of x. So numbers in A1 will start from 0. After the sort on A1 is done, simply re-generate A, by adding x back to each number of A1.

Loading related videos...