Jump to content

Sorting algorithms/Counting sort: Difference between revisions

m
No edit summary
Line 610:
</pre>
=={{header|Elena}}==
ELENA 3.4.x :
<lang elena>import extensions.;
import system'routines.;
extension op
{
countingSort()
= self .clone; ().countingSort(self minimalMember.MinimalMember, self maximalMember).MaximalMember);
countingSort(int min, int max)
[{
Array<int>[] count := V<new int>[](max - min + 1).;
int z := 0.;
count .populate(:i)<(int i =>( 0).;
0 tillfor(selfint length)i do(:= 0, i < self.Length, i += 1) { count[self[i] - min] := count[self[i] - min] += 1 ].};
minfor(int i to:= min, i <= max, do(:i += 1)
[{
while (count[i - min] > 0)
[{
self[z] := i.;
z += 1.;
count[i - min] -:= count[i - min] - 1.
]}
]}
]}
}
public program()
{
[
var list := new Range(0, to:10 repeat(:i).selectBy:(randomGeneratori => randomGenerator.eval(10)); toArray.toArray();
console .printLine("before:", list).asEnumerable());
console .printLine("after :", list .countingSort().asEnumerable())
]}</lang>
{{out}}
<pre>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.