Sorting algorithms/Counting sort: Difference between revisions
added RPL
imported>Arakov |
(added RPL) |
||
(7 intermediate revisions by 4 users not shown) | |||
Line 1,358:
? arr
# value: [0, 0, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 34, 34, 34, 37, 56, 67, 76, 78, 234, 435, 435, 453, 467, 534, 634, 735].diverge()</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
proc countsort min max . d[] .
len count[] max - min + 1
for n in d[]
.
z = 1
for i = min to max
while count[i - min + 1] > 0
d[z] = i
z += 1
count[i - min + 1] -= 1
.
.
.
for i = 1 to 100
d[] &= randint 1000
.
countsort 1 1000 d[]
print d[]
</syntaxhighlight>
=={{header|Eiffel}}==
Line 1,485 ⟶ 1,508:
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import extensions;
import system'routines;
Line 1,499 ⟶ 1,522:
int z := 0;
count.populate::(int i => 0);
for(int i := 0
for(int i := min
{
while (count[i - min] > 0)
Line 1,518 ⟶ 1,541:
public program()
{
var list := new Range(0, 10).selectBy::(i => randomGenerator.nextInt(10)).toArray();
console.printLine("before:", list.asEnumerable());
Line 2,182 ⟶ 2,205:
=={{header|langur}}==
{{works with|langur|0.10}}▼
▲<syntaxhighlight lang="langur">val .countingSort = f(.array) {
for .i of .count { _for ~= .count[.i] * [.i+.min-1] }
▲ val .min, .max = minmax(.array)
▲ var .count = arr .max-.min+1, 0
▲ for .i in .array { .count[.i-.min+1] += 1 }
▲ for .i of .count { _for ~= arr .count[.i], .i+.min-1 }
}
Line 2,195 ⟶ 2,215:
writeln "Original: ", .data
writeln "Sorted : ", .countingSort(.data)
</syntaxhighlight>
{{out}}
Line 3,281 ⟶ 3,302:
return f
</syntaxhighlight>
=={{header|RPL}}==
« { } → 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,590 ⟶ 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
|