Originally Posted by
BlueCactus
It's actually not so hard once you've stared at it for a while.
Basically, you find the minimum and maximum values in the data. You create a counts() array spanning from minimum to maximum. This array then has an element for each possible value in the data. You walk through the data, and count how many times each possible value appears in the data. Thus, if counts(7)=12 then there are 12 occurences of the number 7 in the data. Now, you don't actually have to sort anything, because the counts() array is by definition already in ascending order. So you just step through the counts() array: for each n, you list counts(n) occurences of n, and you're automatically sorted.
An important aspect of VBA in the code provided is the handling of
[vba]for i = min_value to max_value
for j = 1 to counts(i)
...
next j
next i[/vba]
If counts(i)=0, there were no occurences of i in the data. Some versions of BASIC will execute the code contained in the For j = 1 to 0 loop once because j is originally set to 1. VBA does not. It pretests the loop, finds that j>0 and skips it. This eliminates having to enclose the For j loop inside a If counts(i) >0 block.
The code as provided only works with integers, because you can have only integer indices for an array. You could easily modify it to work with fixed decimal places by mupltiplying the data by a factor of 10 (and then rounding) before counting, and dividing it by the same factor at the end. But, this could lead to some pretty huge memory-hungry arrays. (Imagine having min_value = 1, max_value= 100000, with 3 decimal places. That would lead to a counting array with 100,000,000 elements.)