Sorting algorithms/Counting sort: Difference between revisions

(→‎{{header|Ada}}: Changed to meet the task requirements)
(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[]
for .i in .array { .count[.in -. min + 1] += 1 }
.
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 46.x :
<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,; i < self.Length,; i += 1) { count[self[i] - min] := count[self[i] - min] + 1 };
for(int i := min,; i <= max,; i += 1)
{
while (count[i - min] > 0)
Line 1,518 ⟶ 1,541:
public program()
{
var list := new Range(0, 10).selectBy::(i => randomGenerator.evalnextInt(10)).toArray();
console.printLine("before:", list.asEnumerable());
Line 2,182 ⟶ 2,205:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val .countingSort = ffn(.arraylist) {
{{works with|langur|0.10}}
val .min, .max = minmax(.arraylist)
Prior to 0.10, multi-variable declaration/assignment would use parentheses around variable names and values.
var .count = arr[0] * (.max-.min+1, 0)
 
for .i ofin .countlist { _for ~= arr .count[.i-.min+1], .i+.min-= 1 }
<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>
</syntaxhighlight>
 
{{out}}
Line 3,590 ⟶ 3,611:
 
=={{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
885

edits