On the Wikipedia page about Counting Sort, it states:
It is possible to modify the algorithm so that it places the items into sorted order within the same array that was given to it as the input, using only the count array as auxiliary storage; however, the modified in-place version of counting sort is not stable.
I am curious about how such algorithm is implemented, but I don't have access to the source cited. Could somebody explain how it works?
After counting, you have an array of counts for each value, say:
Shift it to the right:
With a cumulative sum, change this into an array of the start positions for each value in the sorted result:
Now you walk through the array, swapping each each item into place at the start position for its value, and incrementing the start position so the next one will go in the right place, but ONLY if the item belongs the same or earlier position. If it belongs in a later position, then just skip it, and the item that belongs there will be swapped in when we get to it (because it must belong in the earlier position).
Swapping will bring a new element into the target position, so repeat at the same position until you find one that isn't swapped.
Here's the algorithm in JavaScript, using values from 0 to 9: