Sorting algorithms/Counting sort: Difference between revisions

Content added Content deleted
No edit summary
Line 610: Line 610:
</pre>
</pre>
=={{header|Elena}}==
=={{header|Elena}}==
ELENA 3.4 :
ELENA 4.x :
<lang elena>import extensions.
<lang elena>import extensions;
import system'routines.
import system'routines;
extension op
extension op
{
{
countingSort
countingSort()
= self clone; countingSort(self minimalMember, self maximalMember).
= self.clone().countingSort(self.MinimalMember, self.MaximalMember);
countingSort(int min, int max)
countingSort(int min, int max)
[
{
Array<int> count := V<int>(max - min + 1).
int[] count := new int[](max - min + 1);
int z := 0.
int z := 0;
count populate(:i)<int>(0).
count.populate:(int i => 0);
0 till(self length) do(:i) [ count[self[i] - min] += 1 ].
for(int i := 0, i < self.Length, i += 1) { count[self[i] - min] := count[self[i] - min] + 1 };
min to:max do(:i)
for(int i := min, i <= max, i += 1)
[
{
while (count[i - min] > 0)
while (count[i - min] > 0)
[
{
self[z] := i.
self[z] := i;
z += 1.
z += 1;
count[i - min] -= 1.
count[i - min] := count[i - min] - 1
]
}
]
}
]
}
}
}
public program
public program()
{
[
var list := 0 to:10 repeat(:i)(randomGenerator eval(10)); toArray.
var list := new Range(0, 10).selectBy:(i => randomGenerator.eval(10)).toArray();
console printLine("before:", list).
console.printLine("before:", list.asEnumerable());
console printLine("after :", list countingSort)
console.printLine("after :", list.countingSort().asEnumerable())
]</lang>
}</lang>
{{out}}
{{out}}
<pre>
<pre>