Talk:Sorting algorithms/Counting sort

From Rosetta Code
Revision as of 19:19, 8 October 2009 by rosettacode>Sgeier (number of elements?)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

number of elements?

There's a slight ambiguity in the task description (I think). The last sentence reads: "sparse arrays may limit the impact of the memory usage" which appears to allow for the use of sparse arrays, i.e. arrays that have indices in the range [min..max] but do NOT actually have (max-min+1) elements. The pseudocode, however, expressly requires "count: array of (max - min + 1) elements" which appears to contradict the prose.

I'm asking because the TCL implementation strikes me as a shade kludgy - I would write the whole thing using associative arrays, which have only the elements that are actually set. I.e. more like a "sparse array". They also have things like [incr arr(i)] so that the 'helper function' wouldn't be needed at all. They do, however require a bunch more time in element access (since there's a hash function under the hood somewhere) and in a sense dilute the purity of the implemented algorithm. On the other hand, they wouldn't obscure the algorithm all that much, as the user can think of the array as having all the element arr[min] to arr[max] even though most of them don't actually exist (i.e. no memory is allocated for them. They come into existence when they're incremented.)

Just putting this out for discussion ... Sgeier 19:19, 8 October 2009 (UTC)