Sorting algorithms/Counting sort: Difference between revisions

added RPL
(added RPL)
 
(3 intermediate revisions by 3 users not shown)
Line 1,360:
 
=={{header|EasyLang}}==
<syntaxhighlight lang=easylang>
proc countsort min max . d[] .
len count[] max - min + 1
Line 1,376:
.
for i = 1 to 100
d[] &= randomrandint 1000
.
countsort 1 1000 d[]
Line 2,205:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val .countingSort = ffn(.list) {
val .min, .max = minmax(.list)
var .count = [0] x* (.max-.min+1)
for .i in .list { .count[.i-.min+1] += 1 }
for .i of .count { _for ~= .count[.i] x* [.i+.min-1] }
}
 
Line 2,215:
 
writeln "Original: ", .data
writeln "Sorted : ", .countingSort(.data)</syntaxhighlight>
</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="ecmascriptwren">var countingSort = Fn.new { |a, min, max|
var count = List.filled(max - min + 1, 0)
for (n in a) count[n - min] = count[n - min] + 1
1,150

edits