Sorting algorithms/Counting sort: Difference between revisions
Sorting algorithms/Counting sort (view source)
Revision as of 23:42, 7 September 2012
, 11 years agoJ: clarify and illustrate an alternative
(J: clarify and illustrate an alternative) |
|||
Line 589:
Alternative implementation:
<lang
And note that this can be simplified if the range is known in advance (which would probably be the case -- this sorting mechanism is practical when we have a small fixed range of values that we are sorting.▼
'''Example:'''
Line 598 ⟶ 597:
csort a
_3 _2 _2 _1 _1 _1 0 1 1 3 3 4 4 4 4 4 5 5 6 6</lang>
▲And note that this can be further simplified if the range is known in advance (which
<lang j>csrt=:2 :0
(m+i.n-m) (+/@(=/)~ # [) ]
)</lang>
Example:
<lang j> (_3 csrt 17) a
_3 _2 _2 _1 _1 _1 0 1 1 3 3 4 4 4 4 4 5 5 6 6</lang>
|