Sorting algorithms/Counting sort: Difference between revisions
Content added Content deleted
(Add Nimrod) |
|||
Line 1,041: | Line 1,041: | ||
-200 -10 -6 0 0 1 2 3 6 7 8 9 10 11 12 13 14 15 16 17 18 20 21 22 23 24 25 38 39 41 42 42 43 43 44 45 46 62 63 77 78 78 79 113 114 |
-200 -10 -6 0 0 1 2 3 6 7 8 9 10 11 12 13 14 15 16 17 18 20 21 22 23 24 25 38 39 41 42 42 43 43 44 45 46 62 63 77 78 78 79 113 114 |
||
</pre> |
</pre> |
||
=={{header|Nimrod}}== |
|||
<lang nimrod>proc countingSort[T](a: var openarray[T]; min, max: int) = |
|||
let range = max - min + 1 |
|||
var count = newSeq[T](range) |
|||
var z = 0 |
|||
for i in 0 .. < a.len: inc count[a[i] - min] |
|||
for i in min .. max: |
|||
for j in 0 .. <count[i - min]: |
|||
a[z] = i |
|||
inc z |
|||
var a = @[5, 3, 1, 7, 4, 1, 1, 20] |
|||
countingSort(a, 1, 20) |
|||
echo a</lang> |
|||
Output: |
|||
<pre>@[1, 1, 1, 3, 4, 5, 7, 20]</pre> |
|||
=={{header|Objeck}}== |
=={{header|Objeck}}== |