Sorting algorithms/Counting sort: Difference between revisions
added RPL
Langurmonkey (talk | contribs) |
(added RPL) |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 1,360:
=={{header|EasyLang}}==
<syntaxhighlight
proc countsort min max . d[] .
len count[] max - min + 1
Line 1,376:
.
for i = 1 to 100
d[] &=
.
countsort 1 1000 d[]
Line 2,205:
=={{header|langur}}==
<syntaxhighlight lang="langur">val .countingSort =
val .min, .max = minmax(.list)
var .count = [0]
for .i in .list { .count[.i-.min+1] += 1 }
for .i of .count { _for ~= .count[.i]
}
Line 2,215:
writeln "Original: ", .data
writeln "Sorted : ", .countingSort(.data)
</syntaxhighlight>
{{out}}
Line 3,301 ⟶ 3,302:
return f
</syntaxhighlight>
=={{header|RPL}}==
{{works with|RPL|HP-49C}}
« { } → in bins
« in « MIN » STREAM DUP
in « MAX » STREAM
'''FOR''' j
'bins'
'''IF''' in j POS
'''THEN''' 0 in + « j == + » STREAM
'''ELSE''' 0 '''END'''
STO+
'''NEXT'''
{ }
1 bins SIZE '''FOR''' j
OVER j + 1 - 'bins' j GET NDUPN →LIST +
'''NEXT''' NIP
» '<span style="color:blue">CSORT</span>' STO
{ -5 1 0 5 7 5 1 2 -3 1 } <span style="color:blue">CSORT</span>
{{out}}
<pre>
1: { -5 -3 0 1 1 1 2 5 5 7 }
</pre>
Counting sort is 17 times slower than the <code>SORT</code> built-in function on an HP-50g.
=={{header|Ruby}}==
Line 3,610 ⟶ 3,636:
=={{header|Wren}}==
<syntaxhighlight lang="
var count = List.filled(max - min + 1, 0)
for (n in a) count[n - min] = count[n - min] + 1
|