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 |
ELENA 4.x : |
||
<lang elena>import extensions |
<lang elena>import extensions; |
||
import system'routines |
import system'routines; |
||
extension op |
extension op |
||
{ |
{ |
||
countingSort |
countingSort() |
||
= self |
= self.clone().countingSort(self.MinimalMember, self.MaximalMember); |
||
countingSort(int min, int max) |
countingSort(int min, int max) |
||
{ |
|||
int[] count := new int[](max - min + 1); |
|||
int z := 0 |
int z := 0; |
||
count |
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) |
while (count[i - min] > 0) |
||
{ |
|||
self[z] := i |
self[z] := i; |
||
z += 1 |
z += 1; |
||
count[i - min] |
count[i - min] := count[i - min] - 1 |
||
} |
|||
} |
|||
} |
|||
} |
} |
||
public program |
public program() |
||
{ |
|||
[ |
|||
var list := 0 |
var list := new Range(0, 10).selectBy:(i => randomGenerator.eval(10)).toArray(); |
||
console |
console.printLine("before:", list.asEnumerable()); |
||
console |
console.printLine("after :", list.countingSort().asEnumerable()) |
||
}</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |