Sorting algorithms/Counting sort: Difference between revisions

J: clarify and illustrate an alternative
(J: clarify and illustrate an alternative)
Line 589:
Alternative implementation:
 
<lang Jj>csort=: (+/@(=/) # ]) >./ (] + 1 i.@+ -) <./</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 wouldcould probablyeasily be the case -- this sorting mechanism is practical when we have a small fixed range of values that we are sorting). Here, we do not need to inspect the data to find min and max values, since they are already known:
 
<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>
 
6,962

edits