Sorting algorithms/Counting sort: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 34: | Line 34: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F countingSort(a, min, max) |
||
V cnt = [0] * (max - min + 1) |
V cnt = [0] * (max - min + 1) |
||
L(x) a |
L(x) a |
||
Line 45: | Line 45: | ||
V data = [9, 7, 10, 2, 9, 7, 4, 3, 10, 2, 7, 10, 2, 1, 3, 8, 7, 3, 9, 5, 8, 5, 1, 6, 3, 7, 5, 4, 6, 9, 9, 6, 6, 10, 2, 4, 5, 2, 8, 2, 2, 5, 2, 9, 3, 3, 5, 7, 8, 4] |
V data = [9, 7, 10, 2, 9, 7, 4, 3, 10, 2, 7, 10, 2, 1, 3, 8, 7, 3, 9, 5, 8, 5, 1, 6, 3, 7, 5, 4, 6, 9, 9, 6, 6, 10, 2, 4, 5, 2, 8, 2, 2, 5, 2, 9, 3, 3, 5, 7, 8, 4] |
||
print(countingSort(data, min(data), max(data)) == sorted(data))</ |
print(countingSort(data, min(data), max(data)) == sorted(data))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 53: | Line 53: | ||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
< |
<syntaxhighlight lang="360asm">* Counting sort - 18/04/2020 |
||
COUNTS CSECT |
COUNTS CSECT |
||
USING COUNTS,R13 base register |
USING COUNTS,R13 base register |
||
Line 116: | Line 116: | ||
COUNT DC 200F'0' count |
COUNT DC 200F'0' count |
||
REGEQU |
REGEQU |
||
END COUNTS </ |
END COUNTS </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 124: | Line 124: | ||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
||
<syntaxhighlight lang="aarch64 assembly"> |
|||
<lang AArch64 Assembly> |
|||
/* ARM assembly AARCH64 Raspberry PI 3B */ |
/* ARM assembly AARCH64 Raspberry PI 3B */ |
||
/* program countSort64.s */ |
/* program countSort64.s */ |
||
Line 342: | Line 342: | ||
/* for this file see task include a file in language AArch64 assembly */ |
/* for this file see task include a file in language AArch64 assembly */ |
||
.include "../includeARM64.inc" |
.include "../includeARM64.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">DEFINE MAXSIZE="100" |
||
PROC PrintArray(INT ARRAY a INT size) |
PROC PrintArray(INT ARRAY a INT size) |
||
Line 408: | Line 408: | ||
Test(c,8,101,108) |
Test(c,8,101,108) |
||
Test(d,12,-1,1) |
Test(d,12,-1,1) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Counting_sort.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Counting_sort.png Screenshot from Atari 8-bit computer] |
||
Line 434: | Line 434: | ||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
< |
<syntaxhighlight lang="actionscript">function countingSort(array:Array, min:int, max:int) |
||
{ |
{ |
||
var count:Array = new Array(array.length); |
var count:Array = new Array(array.length); |
||
Line 449: | Line 449: | ||
} |
} |
||
return array; |
return array; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
Given that we know the range of data, the problem really reduces to initializing the array to the ordered range of values. The input order is irrelevant. |
Given that we know the range of data, the problem really reduces to initializing the array to the ordered range of values. The input order is irrelevant. |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_Io; use Ada.Text_Io; |
||
procedure Counting_Sort is |
procedure Counting_Sort is |
||
Line 470: | Line 470: | ||
end loop; |
end loop; |
||
New_Line; |
New_Line; |
||
end Counting_Sort;</ |
end Counting_Sort;</syntaxhighlight> |
||
===Output=== |
===Output=== |
||
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
||
Line 485: | Line 485: | ||
<br> |
<br> |
||
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}} |
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}} |
||
< |
<syntaxhighlight lang="algol68">PROC counting sort mm = (REF[]INT array, INT min, max)VOID: |
||
( |
( |
||
INT z := LWB array - 1; |
INT z := LWB array - 1; |
||
Line 524: | Line 524: | ||
FOR i TO UPB ages DO print((" ", whole(ages[i],0))) OD; |
FOR i TO UPB ages DO print((" ", whole(ages[i],0))) OD; |
||
print(new line) |
print(new line) |
||
)</ |
)</syntaxhighlight> |
||
Sample output: |
Sample output: |
||
Line 530: | Line 530: | ||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
<syntaxhighlight lang="arm assembly"> |
|||
<lang ARM Assembly> |
|||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
/* program countSort.s */ |
/* program countSort.s */ |
||
Line 734: | Line 734: | ||
/***************************************************/ |
/***************************************************/ |
||
.include "../affichage.inc" |
.include "../affichage.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">countingSort: function [items, minimum, maximum][ |
||
a: new items |
a: new items |
||
rng: inc maximum - minimum |
rng: inc maximum - minimum |
||
Line 757: | Line 757: | ||
] |
] |
||
print countingSort [3 1 2 8 5 7 9 4 6] 1 9</ |
print countingSort [3 1 2 8 5 7 9 4 6] 1 9</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 765: | Line 765: | ||
=={{header|ATS}}== |
=={{header|ATS}}== |
||
< |
<syntaxhighlight lang="ats">#include "share/atspre_staload.hats" |
||
(* My ATS solution to the radix sort task includes a counting sort for |
(* My ATS solution to the radix sort task includes a counting sort for |
||
Line 952: | Line 952: | ||
for (i := 0; i <> 8; i := succ i) |
for (i := 0; i <> 8; i := succ i) |
||
println! (arr[i].0, " -> ", arr[i].1) |
println! (arr[i].0, " -> ", arr[i].1) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 967: | Line 967: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276465.html#276465 forum] |
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276465.html#276465 forum] |
||
< |
<syntaxhighlight lang="autohotkey">MsgBox % CountingSort("-1,1,1,0,-1",-1,1) |
||
CountingSort(ints,min,max) { |
CountingSort(ints,min,max) { |
||
Line 980: | Line 980: | ||
} |
} |
||
Return SubStr(t,2) |
Return SubStr(t,2) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |
||
<syntaxhighlight lang="basic256"> |
|||
<lang BASIC256> |
|||
# counting sort |
# counting sort |
||
Line 1,021: | Line 1,021: | ||
next i |
next i |
||
print |
print |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
< |
<syntaxhighlight lang="bbcbasic"> DIM test%(9) |
||
test%() = 4, 65, 2, -31, 0, 99, 2, 83, 782, 1 |
test%() = 4, 65, 2, -31, 0, 99, 2, 83, 782, 1 |
||
PROCcountingsort(test%(), -31, 782) |
PROCcountingsort(test%(), -31, 782) |
||
Line 1,046: | Line 1,046: | ||
ENDWHILE |
ENDWHILE |
||
NEXT |
NEXT |
||
ENDPROC</ |
ENDPROC</syntaxhighlight> |
||
'''Output:''' |
'''Output:''' |
||
<pre> |
<pre> |
||
Line 1,053: | Line 1,053: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 1,087: | Line 1,087: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Testing (we suppose the oldest human being is less than 140 years old). |
Testing (we suppose the oldest human being is less than 140 years old). |
||
< |
<syntaxhighlight lang="c">#define N 100 |
||
#define MAX_AGE 140 |
#define MAX_AGE 140 |
||
int main() |
int main() |
||
Line 1,101: | Line 1,101: | ||
for(i=0; i < N; i++) printf("%d\n", ages[i]); |
for(i=0; i < N; i++) printf("%d\n", ages[i]); |
||
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Linq; |
using System.Linq; |
||
Line 1,139: | Line 1,139: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <iostream> |
#include <iostream> |
||
#include <time.h> |
#include <time.h> |
||
Line 1,207: | Line 1,207: | ||
} |
} |
||
//------------------------------------------------------------------------------ |
//------------------------------------------------------------------------------ |
||
</ |
</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,220: | Line 1,220: | ||
Uses C++11. Compile with |
Uses C++11. Compile with |
||
g++ -std=c++11 counting.cpp |
g++ -std=c++11 counting.cpp |
||
< |
<syntaxhighlight lang="cpp">#include <algorithm> |
||
#include <iterator> |
#include <iterator> |
||
#include <iostream> |
#include <iostream> |
||
Line 1,249: | Line 1,249: | ||
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " ")); |
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " ")); |
||
std::cout << "\n"; |
std::cout << "\n"; |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,258: | Line 1,258: | ||
Straightforward implementation of counting sort. By using <code>[http://www.lispworks.com/documentation/HyperSpec/Body/f_map.htm map]</code> and <code>[http://www.lispworks.com/documentation/HyperSpec/Body/f_map_in.htm map-into]</code>, counting sort can work efficiently on both lists and vectors. The closure given as the second argument to <code>map-into</code> returns the sorted elements of sequence. Because <code>map-into</code> will only call the function as many times as necessary to re-populate sequence, there is no need for bounds checking. <code>counts</code> is declared to have dynamic-extent and so a compiler might stack allocate it. |
Straightforward implementation of counting sort. By using <code>[http://www.lispworks.com/documentation/HyperSpec/Body/f_map.htm map]</code> and <code>[http://www.lispworks.com/documentation/HyperSpec/Body/f_map_in.htm map-into]</code>, counting sort can work efficiently on both lists and vectors. The closure given as the second argument to <code>map-into</code> returns the sorted elements of sequence. Because <code>map-into</code> will only call the function as many times as necessary to re-populate sequence, there is no need for bounds checking. <code>counts</code> is declared to have dynamic-extent and so a compiler might stack allocate it. |
||
< |
<syntaxhighlight lang="lisp">(defun counting-sort (sequence &optional (min (reduce #'min sequence)) |
||
(max (reduce #'max sequence))) |
(max (reduce #'max sequence))) |
||
(let ((i 0) |
(let ((i 0) |
||
Line 1,269: | Line 1,269: | ||
(incf i)) |
(incf i)) |
||
(decf (aref counts i)) |
(decf (aref counts i)) |
||
(+ i min)))))</ |
(+ i min)))))</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.algorithm; |
||
void countingSort(int[] array, in size_t min, in size_t max) |
void countingSort(int[] array, in size_t min, in size_t max) |
||
Line 1,298: | Line 1,298: | ||
countingSort(data, dataMin, dataMax); |
countingSort(data, dataMin, dataMax); |
||
assert(isSorted(data)); |
assert(isSorted(data)); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
Line 1,305: | Line 1,305: | ||
Straightforward implementation, no particularly interesting characteristics. |
Straightforward implementation, no particularly interesting characteristics. |
||
< |
<syntaxhighlight lang="e">def countingSort(array, min, max) { |
||
def counts := ([0] * (max - min + 1)).diverge() |
def counts := ([0] * (max - min + 1)).diverge() |
||
for elem in array { |
for elem in array { |
||
Line 1,317: | Line 1,317: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre style="height:15ex;overflow:scroll">? def arr := [34,6,8,7,4,3,56,7,8,4,3,5,7,8,6,4,4,67,9,0,0,76,467,453,34,435,37,4,34,234,435,3,2,7,4,634,534,735,5,4,6,78,4].diverge() |
<pre style="height:15ex;overflow:scroll">? def arr := [34,6,8,7,4,3,56,7,8,4,3,5,7,8,6,4,4,67,9,0,0,76,467,453,34,435,37,4,34,234,435,3,2,7,4,634,534,735,5,4,6,78,4].diverge() |
||
Line 1,328: | Line 1,328: | ||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
<syntaxhighlight lang="eiffel"> |
|||
<lang Eiffel> |
|||
class |
class |
||
Line 1,399: | Line 1,399: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
TEST: |
TEST: |
||
<syntaxhighlight lang="eiffel"> |
|||
<lang Eiffel> |
|||
class |
class |
||
APPLICATION |
APPLICATION |
||
Line 1,442: | Line 1,442: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,453: | Line 1,453: | ||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 4.x : |
ELENA 4.x : |
||
< |
<syntaxhighlight lang="elena">import extensions; |
||
import system'routines; |
import system'routines; |
||
Line 1,489: | Line 1,489: | ||
console.printLine("before:", list.asEnumerable()); |
console.printLine("before:", list.asEnumerable()); |
||
console.printLine("after :", list.countingSort().asEnumerable()) |
console.printLine("after :", list.countingSort().asEnumerable()) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,498: | Line 1,498: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{works with|Elixir|1.1}} |
{{works with|Elixir|1.1}} |
||
< |
<syntaxhighlight lang="elixir">defmodule Sort do |
||
def counting_sort([]), do: [] |
def counting_sort([]), do: [] |
||
def counting_sort(list) do |
def counting_sort(list) do |
||
Line 1,511: | Line 1,511: | ||
end |
end |
||
IO.inspect Sort.counting_sort([1,-2,-3,2,1,-5,5,5,4,5,9])</ |
IO.inspect Sort.counting_sort([1,-2,-3,2,1,-5,5,5,4,5,9])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,521: | Line 1,521: | ||
{{works with|Fortran|95 and later}} |
{{works with|Fortran|95 and later}} |
||
< |
<syntaxhighlight lang="fortran">module CountingSort |
||
implicit none |
implicit none |
||
Line 1,563: | Line 1,563: | ||
end subroutine counting_sort_mm |
end subroutine counting_sort_mm |
||
end module CountingSort</ |
end module CountingSort</syntaxhighlight> |
||
Testing: |
Testing: |
||
< |
<syntaxhighlight lang="fortran">program test |
||
use CountingSort |
use CountingSort |
||
implicit none |
implicit none |
||
Line 1,583: | Line 1,583: | ||
write(*,'(I4)') ages |
write(*,'(I4)') ages |
||
end program test</ |
end program test</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
Function findMax(array() As Integer) As Integer |
Function findMax(array() As Integer) As Integer |
||
Line 1,644: | Line 1,644: | ||
Print |
Print |
||
Print "Press any key to quit" |
Print "Press any key to quit" |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,654: | Line 1,654: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
This version follows the task pseudocode above, with one more optimization. |
This version follows the task pseudocode above, with one more optimization. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,698: | Line 1,698: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
This version follows the WP pseudocode. It can be adapted to sort items other than integers. |
This version follows the WP pseudocode. It can be adapted to sort items other than integers. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,749: | Line 1,749: | ||
} |
} |
||
copy(a, output) |
copy(a, output) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Solution: |
Solution: |
||
< |
<syntaxhighlight lang="groovy">def countingSort = { array -> |
||
def max = array.max() |
def max = array.max() |
||
def min = array.min() |
def min = array.min() |
||
Line 1,760: | Line 1,760: | ||
array.each { count[it] ++ } |
array.each { count[it] ++ } |
||
(min..max).findAll{ count[it] }.collect{ [it]*count[it] }.flatten() |
(min..max).findAll{ count[it] }.collect{ [it]*count[it] }.flatten() |
||
}</ |
}</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<syntaxhighlight lang="groovy">println countingSort([23,76,99,58,97,57,35,89,51,38,95,92,24,46,31,24,14,12,57,78,4]) |
||
println countingSort([88,18,31,44,4,0,8,81,14,78,20,76,84,33,73,75,82,5,62,70,12,7,1]) |
println countingSort([88,18,31,44,4,0,8,81,14,78,20,76,84,33,73,75,82,5,62,70,12,7,1]) |
||
Line 1,769: | Line 1,769: | ||
println countingSort([34,6,8,7,4,3,56,7,8,4,3,5,7,8,6,4,4,67,9,0,0,76,467,453,34,435,37,4,34,234,435,3,2,7,4,634,534,-735,5,4,6,78,4]) |
println countingSort([34,6,8,7,4,3,56,7,8,4,3,5,7,8,6,4,4,67,9,0,0,76,467,453,34,435,37,4,34,234,435,3,2,7,4,634,534,-735,5,4,6,78,4]) |
||
// slo-o-o-o-ow due to unnecessarily large counting array |
// slo-o-o-o-ow due to unnecessarily large counting array |
||
println countingSort([10000033,10000006,10000008,10000009,10000013,10000031,10000013,10000032,10000023,10000023,10000011,10000012,10000021])</ |
println countingSort([10000033,10000006,10000008,10000009,10000013,10000031,10000013,10000032,10000023,10000023,10000011,10000012,10000021])</syntaxhighlight> |
||
Output: |
Output: |
||
Line 1,781: | Line 1,781: | ||
We use lists for input and output rather than arrays, since lists are used more often in Haskell. |
We use lists for input and output rather than arrays, since lists are used more often in Haskell. |
||
< |
<syntaxhighlight lang="haskell">import Data.Array |
||
countingSort :: (Ix n) => [n] -> n -> n -> [n] |
countingSort :: (Ix n) => [n] -> n -> n -> [n] |
||
countingSort l lo hi = concatMap (uncurry $ flip replicate) count |
countingSort l lo hi = concatMap (uncurry $ flip replicate) count |
||
where count = assocs . accumArray (+) 0 (lo, hi) . map (\i -> (i, 1)) $ l</ |
where count = assocs . accumArray (+) 0 (lo, hi) . map (\i -> (i, 1)) $ l</syntaxhighlight> |
||
=={{header|Haxe}}== |
=={{header|Haxe}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="haxe">class CountingSort { |
||
public static function sort(arr:Array<Int>) { |
public static function sort(arr:Array<Int>) { |
||
var min = arr[0], max = arr[0]; |
var min = arr[0], max = arr[0]; |
||
Line 1,821: | Line 1,821: | ||
Sys.println('Sorted Integers: ' + integerArray); |
Sys.println('Sorted Integers: ' + integerArray); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,832: | Line 1,832: | ||
The following example is hopefully in the spirit of a counting sort using a hash table as a substituted for a sparse array. Simply translating the pseudo-code would be very un-Iconish (as opposed to Uniconish). |
The following example is hopefully in the spirit of a counting sort using a hash table as a substituted for a sparse array. Simply translating the pseudo-code would be very un-Iconish (as opposed to Uniconish). |
||
< |
<syntaxhighlight lang="icon">procedure main() #: demonstrate various ways to sort a list and string |
||
write("Sorting Demo using ",image(countingsort)) |
write("Sorting Demo using ",image(countingsort)) |
||
writes(" on list : ") |
writes(" on list : ") |
||
Line 1,854: | Line 1,854: | ||
every put(X := [],( 1 to T[i := lower to upper], i) ) # reconstitute with correct order and count |
every put(X := [],( 1 to T[i := lower to upper], i) ) # reconstitute with correct order and count |
||
return X |
return X |
||
end</ |
end</syntaxhighlight> |
||
Note: This example relies on [[Sorting_algorithms/Bubble_sort#Icon| the supporting procedures 'display sort', and 'writex' from Bubble Sort]]. |
Note: This example relies on [[Sorting_algorithms/Bubble_sort#Icon| the supporting procedures 'display sort', and 'writex' from Bubble Sort]]. |
||
Line 1,864: | Line 1,864: | ||
=={{header|Io}}== |
=={{header|Io}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="io">List do( |
||
countingSort := method(min, max, |
countingSort := method(min, max, |
||
count := list() setSize(max - min + 1) mapInPlace(0) |
count := list() setSize(max - min + 1) mapInPlace(0) |
||
Line 1,887: | Line 1,887: | ||
l := list(2, 3, -4, 5, 1) |
l := list(2, 3, -4, 5, 1) |
||
l countingSortInPlace println # ==> list(-4, 1, 2, 3, 5)</ |
l countingSortInPlace println # ==> list(-4, 1, 2, 3, 5)</syntaxhighlight> |
||
A more functional-like version: |
A more functional-like version: |
||
< |
<syntaxhighlight lang="io">List do( |
||
fill := method(x, size, |
fill := method(x, size, |
||
/* Resizes list to a given size and fills it with a given value. */ |
/* Resizes list to a given size and fills it with a given value. */ |
||
Line 1,912: | Line 1,912: | ||
l := list(2, 3, -4, 5, 1) |
l := list(2, 3, -4, 5, 1) |
||
l countingSortInPlace println # ==> list(-4, 1, 2, 3, 5)</ |
l countingSortInPlace println # ==> list(-4, 1, 2, 3, 5)</syntaxhighlight> |
||
=={{header|IS-BASIC}}== |
=={{header|IS-BASIC}}== |
||
<syntaxhighlight lang="is-basic"> |
|||
<lang IS-BASIC> |
|||
100 PROGRAM "CountSrt.bas" |
100 PROGRAM "CountSrt.bas" |
||
110 RANDOMIZE |
110 RANDOMIZE |
||
Line 1,962: | Line 1,962: | ||
540 LOOP |
540 LOOP |
||
550 NEXT |
550 NEXT |
||
560 END DEF</ |
560 END DEF</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
{{eff note|J|/:~}} |
{{eff note|J|/:~}} |
||
< |
<syntaxhighlight lang="j">csort =: monad define |
||
min =. <./y |
min =. <./y |
||
cnt =. 0 $~ 1+(>./y)-min |
cnt =. 0 $~ 1+(>./y)-min |
||
Line 1,973: | Line 1,973: | ||
end. |
end. |
||
cnt # min+i.#cnt |
cnt # min+i.#cnt |
||
)</ |
)</syntaxhighlight> |
||
Alternative implementation: |
Alternative implementation: |
||
< |
<syntaxhighlight lang="j">csort=: (+/@(=/) # ]) >./ (] + 1 i.@+ -) <./</syntaxhighlight> |
||
'''Example:''' |
'''Example:''' |
||
< |
<syntaxhighlight lang="j"> ] a =. _3 + 20 ?@$ 10 |
||
_2 _2 6 _1 1 6 _1 4 4 1 4 4 5 _3 5 3 0 _1 3 4 |
_2 _2 6 _1 1 6 _1 4 4 1 4 4 5 _3 5 3 0 _1 3 4 |
||
csort a |
csort a |
||
_3 _2 _2 _1 _1 _1 0 1 1 3 3 4 4 4 4 4 5 5 6 6</ |
_3 _2 _2 _1 _1 _1 0 1 1 3 3 4 4 4 4 4 5 5 6 6</syntaxhighlight> |
||
And note that this can be further simplified if the range is known in advance (which could easily be the case -- this sorting mechanism is practical when we have a small fixed range of values that we are sorting). Here, we do not need to inspect the data to find min and max values, since they are already known: |
And note that this can be further simplified if the range is known in advance (which could easily be the case -- this sorting mechanism is practical when we have a small fixed range of values that we are sorting). Here, we do not need to inspect the data to find min and max values, since they are already known: |
||
< |
<syntaxhighlight lang="j">csrt=:2 :0 |
||
(m+i.n-m) (+/@(=/)~ # [) ] |
(m+i.n-m) (+/@(=/)~ # [) ] |
||
)</ |
)</syntaxhighlight> |
||
or |
or |
||
< |
<syntaxhighlight lang="j">csrt=:2 :0 |
||
(+/@(=/) # ])&(m+i.n-m) |
(+/@(=/) # ])&(m+i.n-m) |
||
)</ |
)</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang="j"> (_3 csrt 17) a |
||
_3 _2 _2 _1 _1 _1 0 1 1 3 3 4 4 4 4 4 5 5 6 6</ |
_3 _2 _2 _1 _1 _1 0 1 1 3 3 4 4 4 4 4 5 5 6 6</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|1.5+}} |
{{works with|Java|1.5+}} |
||
< |
<syntaxhighlight lang="java5">public static void countingSort(int[] array, int min, int max){ |
||
int[] count= new int[max - min + 1]; |
int[] count= new int[max - min + 1]; |
||
for(int number : array){ |
for(int number : array){ |
||
Line 2,019: | Line 2,019: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript">var countSort = function(arr, min, max) { |
||
var i, z = 0, count = []; |
var i, z = 0, count = []; |
||
Line 2,040: | Line 2,040: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Testing: |
Testing: |
||
< |
<syntaxhighlight lang="javascript">// Line breaks are in HTML |
||
var i, ages = []; |
var i, ages = []; |
||
Line 2,056: | Line 2,056: | ||
for (i = 0; i < 100; i++) { |
for (i = 0; i < 100; i++) { |
||
document.write(ages[i] + "<br />"); |
document.write(ages[i] + "<br />"); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 2,065: | Line 2,065: | ||
object is used instead. This ensures the space requirement is just O(length). In jq, this approach is both time and space |
object is used instead. This ensures the space requirement is just O(length). In jq, this approach is both time and space |
||
efficient, except for the small cost of converting integers to strings, which is necessary because JSON keys must be strings. |
efficient, except for the small cost of converting integers to strings, which is necessary because JSON keys must be strings. |
||
< |
<syntaxhighlight lang="jq">def countingSort(min; max): |
||
. as $in |
. as $in |
||
| reduce range(0;length) as $i |
| reduce range(0;length) as $i |
||
Line 2,079: | Line 2,079: | ||
else reduce range(0; $hash[$s]) as $j (.; . + [$i]) |
else reduce range(0; $hash[$s]) as $j (.; . + [$i]) |
||
end |
end |
||
);</ |
);</syntaxhighlight> |
||
'''Example''': |
'''Example''': |
||
< |
<syntaxhighlight lang="jq"> [1,2,1,4,0,10] | countingSort(0;10)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<syntaxhighlight lang="sh"> |
|||
<lang sh> |
|||
$ jq -M -c -n -f counting_sort.jq |
$ jq -M -c -n -f counting_sort.jq |
||
[0,1,1,2,4,10]</ |
[0,1,1,2,4,10]</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Line 2,091: | Line 2,091: | ||
This is a translation of the pseudocode presented in the task description, accounting for the fact that Julia arrays start indexing at 1 rather than zero and taking care to return a result of the same type as the input. Note that <code>cnt</code> has the machine's standard integer type (typically <code>Int64</code>), which need not match that of the input. |
This is a translation of the pseudocode presented in the task description, accounting for the fact that Julia arrays start indexing at 1 rather than zero and taking care to return a result of the same type as the input. Note that <code>cnt</code> has the machine's standard integer type (typically <code>Int64</code>), which need not match that of the input. |
||
< |
<syntaxhighlight lang="julia">function countsort(a::Vector{<:Integer}) |
||
lo, hi = extrema(a) |
lo, hi = extrema(a) |
||
b = zeros(a) |
b = zeros(a) |
||
Line 2,110: | Line 2,110: | ||
println("# unsorted bytes: $v\n -> sorted bytes: $(countsort(v))") |
println("# unsorted bytes: $v\n -> sorted bytes: $(countsort(v))") |
||
v = rand(1:2 ^ 10, 20) |
v = rand(1:2 ^ 10, 20) |
||
println("# unsorted integers: $v\n -> sorted integers: $(countsort(v))")</ |
println("# unsorted integers: $v\n -> sorted integers: $(countsort(v))")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,119: | Line 2,119: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.1.0 |
||
fun countingSort(array: IntArray) { |
fun countingSort(array: IntArray) { |
||
Line 2,140: | Line 2,140: | ||
countingSort(array) |
countingSort(array) |
||
println("Sorted : ${array.asList()}") |
println("Sorted : ${array.asList()}") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,152: | Line 2,152: | ||
Prior to 0.10, multi-variable declaration/assignment would use parentheses around variable names and values. |
Prior to 0.10, multi-variable declaration/assignment would use parentheses around variable names and values. |
||
< |
<syntaxhighlight lang="langur">val .countingSort = f(.array) { |
||
val .min, .max = minmax(.array) |
val .min, .max = minmax(.array) |
||
var .count = arr .max-.min+1, 0 |
var .count = arr .max-.min+1, 0 |
||
Line 2,162: | Line 2,162: | ||
writeln "Original: ", .data |
writeln "Original: ", .data |
||
writeln "Sorted : ", .countingSort(.data)</ |
writeln "Sorted : ", .countingSort(.data)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,169: | Line 2,169: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">function CountingSort( f ) |
||
local min, max = math.min( unpack(f) ), math.max( unpack(f) ) |
local min, max = math.min( unpack(f) ), math.max( unpack(f) ) |
||
local count = {} |
local count = {} |
||
Line 2,198: | Line 2,198: | ||
for i in next, f do |
for i in next, f do |
||
print( f[i] ) |
print( f[i] ) |
||
end</ |
end</syntaxhighlight> |
||
=={{header|M4}}== |
=={{header|M4}}== |
||
< |
<syntaxhighlight lang="m4">divert(-1) |
||
define(`randSeed',141592653) |
define(`randSeed',141592653) |
||
Line 2,236: | Line 2,236: | ||
show(`a') |
show(`a') |
||
countingsort(`a',0,99) |
countingsort(`a',0,99) |
||
show(`a')</ |
show(`a')</syntaxhighlight> |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">countingSort[list_] := Module[{minElem, maxElem, count, z, number}, |
||
minElem = Min[list]; maxElem = Max[list]; |
minElem = Min[list]; maxElem = Max[list]; |
||
count = ConstantArray[0, (maxElem - minElem + 1)]; |
count = ConstantArray[0, (maxElem - minElem + 1)]; |
||
Line 2,250: | Line 2,250: | ||
count[[i - minElem + 1]] = count[[i - minElem + 1]] - 1;] |
count[[i - minElem + 1]] = count[[i - minElem + 1]] - 1;] |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
<pre>countingSort@{2, 3, 1, 5, 7, 6} |
<pre>countingSort@{2, 3, 1, 5, 7, 6} |
||
->{1, 2, 3, 5, 6, 7}</pre> |
->{1, 2, 3, 5, 6, 7}</pre> |
||
Line 2,257: | Line 2,257: | ||
This is a direct translation of the pseudo-code, except to compensate for MATLAB using 1 based arrays. |
This is a direct translation of the pseudo-code, except to compensate for MATLAB using 1 based arrays. |
||
< |
<syntaxhighlight lang="matlab">function list = countingSort(list) |
||
minElem = min(list); |
minElem = min(list); |
||
Line 2,278: | Line 2,278: | ||
end |
end |
||
end %countingSort</ |
end %countingSort</syntaxhighlight> |
||
Sample Usage: |
Sample Usage: |
||
< |
<syntaxhighlight lang="matlab">>> countingSort([4 3 1 5 6 2]) |
||
ans = |
ans = |
||
1 2 3 4 5 6</ |
1 2 3 4 5 6</syntaxhighlight> |
||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
<syntaxhighlight lang="maxscript"> |
|||
<lang MAXScript> |
|||
fn countingSort arr = |
fn countingSort arr = |
||
( |
( |
||
Line 2,311: | Line 2,311: | ||
) |
) |
||
return arr |
return arr |
||
)</ |
)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<syntaxhighlight lang="maxscript"> |
|||
<lang MAXScript> |
|||
a = for i in 1 to 15 collect random 1 30 |
a = for i in 1 to 15 collect random 1 30 |
||
#(7, 1, 6, 16, 27, 11, 24, 16, 25, 11, 22, 7, 28, 15, 17) |
#(7, 1, 6, 16, 27, 11, 24, 16, 25, 11, 22, 7, 28, 15, 17) |
||
countingSort a |
countingSort a |
||
#(1, 6, 7, 7, 11, 11, 15, 16, 16, 17, 22, 24, 25, 27, 28) |
#(1, 6, 7, 7, 11, 11, 15, 16, 16, 17, 22, 24, 25, 27, 28) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
< |
<syntaxhighlight lang="modula3">MODULE Counting EXPORTS Main; |
||
IMPORT IO, Fmt; |
IMPORT IO, Fmt; |
||
Line 2,361: | Line 2,361: | ||
END; |
END; |
||
IO.Put("\n"); |
IO.Put("\n"); |
||
END Counting.</ |
END Counting.</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 2,370: | Line 2,370: | ||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="nanoquery">def countingSort(array, min, max) |
||
count = {0} * (max - min + 1) |
count = {0} * (max - min + 1) |
||
Line 2,385: | Line 2,385: | ||
end |
end |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
===Version 1=== |
===Version 1=== |
||
An almost direct implementation of the pseudocode. |
An almost direct implementation of the pseudocode. |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref savelog symbols binary |
options replace format comments java crossref savelog symbols binary |
||
Line 2,459: | Line 2,459: | ||
return array |
return array |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre style="overflow: scroll;"> |
<pre style="overflow: scroll;"> |
||
Line 2,468: | Line 2,468: | ||
===Version 2=== |
===Version 2=== |
||
A more Rexx-like (and shorter) version. Due to NetRexx's built in indexed string capability, negative values are also easily supported. |
A more Rexx-like (and shorter) version. Due to NetRexx's built in indexed string capability, negative values are also easily supported. |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols nobinary |
options replace format comments java crossref symbols nobinary |
||
Line 2,518: | Line 2,518: | ||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,526: | Line 2,526: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">proc countingSort[T](a: var openarray[T]; min, max: int) = |
||
let range = max - min + 1 |
let range = max - min + 1 |
||
var count = newSeq[T](range) |
var count = newSeq[T](range) |
||
Line 2,540: | Line 2,540: | ||
var a = @[5, 3, 1, 7, 4, 1, 1, 20] |
var a = @[5, 3, 1, 7, 4, 1, 1, 20] |
||
countingSort(a, 1, 20) |
countingSort(a, 1, 20) |
||
echo a</ |
echo a</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>@[1, 1, 1, 3, 4, 5, 7, 20]</pre> |
<pre>@[1, 1, 1, 3, 4, 5, 7, 20]</pre> |
||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck"> |
||
bundle Default { |
bundle Default { |
||
class Cocktail { |
class Cocktail { |
||
Line 2,576: | Line 2,576: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
For arrays: |
For arrays: |
||
< |
<syntaxhighlight lang="ocaml">let counting_sort_array arr lo hi = |
||
let count = Array.make (hi-lo+1) 0 in |
let count = Array.make (hi-lo+1) 0 in |
||
Array.iter (fun i -> count.(i-lo) <- count.(i-lo) + 1) arr; |
Array.iter (fun i -> count.(i-lo) <- count.(i-lo) + 1) arr; |
||
Array.concat (Array.to_list (Array.mapi (fun i x -> Array.make x (lo+i)) count))</ |
Array.concat (Array.to_list (Array.mapi (fun i x -> Array.make x (lo+i)) count))</syntaxhighlight> |
||
=={{header|Octave}}== |
=={{header|Octave}}== |
||
This implements the same algorithm but in a more compact way (using the same loop to count and to ''update'' the sorted vector). This implementation is ''elegant'' (and possible since the sort is not done "in place"), but not so efficient on machines that can't parallelize some operations (the vector <tt>arr</tt> is scanned for every value between <tt>minval</tt> and <tt>maxval</tt>) |
This implements the same algorithm but in a more compact way (using the same loop to count and to ''update'' the sorted vector). This implementation is ''elegant'' (and possible since the sort is not done "in place"), but not so efficient on machines that can't parallelize some operations (the vector <tt>arr</tt> is scanned for every value between <tt>minval</tt> and <tt>maxval</tt>) |
||
< |
<syntaxhighlight lang="octave">function r = counting_sort(arr, minval, maxval) |
||
r = arr; |
r = arr; |
||
z = 1; |
z = 1; |
||
Line 2,596: | Line 2,596: | ||
endwhile |
endwhile |
||
endfor |
endfor |
||
endfunction</ |
endfunction</syntaxhighlight> |
||
Testing: |
Testing: |
||
< |
<syntaxhighlight lang="octave">ages = unidrnd(140, 100, 1); |
||
sorted = counting_sort(ages, 0, 140); |
sorted = counting_sort(ages, 0, 140); |
||
disp(sorted);</ |
disp(sorted);</syntaxhighlight> |
||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
Using arrays as in the original algorithm. The implementation is slightly simpler because arrays can start with an arbitrary index in Oz. |
Using arrays as in the original algorithm. The implementation is slightly simpler because arrays can start with an arbitrary index in Oz. |
||
< |
<syntaxhighlight lang="oz">declare |
||
proc {CountingSort Arr Min Max} |
proc {CountingSort Arr Min Max} |
||
Count = {Array.new Min Max 0} |
Count = {Array.new Min Max 0} |
||
Line 2,629: | Line 2,629: | ||
in |
in |
||
{CountingSort A 1 9} |
{CountingSort A 1 9} |
||
{Show {Array.toRecord unit A}}</ |
{Show {Array.toRecord unit A}}</syntaxhighlight> |
||
Using lists for input and output and a dictionary as a sparse array: |
Using lists for input and output and a dictionary as a sparse array: |
||
< |
<syntaxhighlight lang="oz">declare |
||
fun {CountingSort Xs} |
fun {CountingSort Xs} |
||
Count = {Dictionary.new} |
Count = {Dictionary.new} |
||
Line 2,652: | Line 2,652: | ||
end |
end |
||
in |
in |
||
{Show {CountingSort [3 1 4 1 5 9 2 6 5]}}</ |
{Show {CountingSort [3 1 4 1 5 9 2 6 5]}}</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">countingSort(v,mn,mx)={ |
||
my(u=vector(#v),i=0); |
my(u=vector(#v),i=0); |
||
for(n=mn,mx, |
for(n=mn,mx, |
||
Line 2,661: | Line 2,661: | ||
); |
); |
||
u |
u |
||
};</ |
};</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
< |
<syntaxhighlight lang="pascal">program CountingSort; |
||
procedure counting_sort(var arr : Array of Integer; n, min, max : Integer); |
procedure counting_sort(var arr : Array of Integer; n, min, max : Integer); |
||
Line 2,695: | Line 2,695: | ||
for i := 0 to 99 do |
for i := 0 to 99 do |
||
writeln(ages[i]); |
writeln(ages[i]); |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#! /usr/bin/perl |
||
use strict; |
use strict; |
||
Line 2,711: | Line 2,711: | ||
my $i = $min; |
my $i = $min; |
||
@$a = map {($i++) x $_} @cnt; |
@$a = map {($i++) x $_} @cnt; |
||
}</ |
}</syntaxhighlight> |
||
Testing: |
Testing: |
||
< |
<syntaxhighlight lang="perl">my @ages = map {int(rand(140))} 1 .. 100; |
||
counting_sort(\@ages, 0, 140); |
counting_sort(\@ages, 0, 140); |
||
print join("\n", @ages), "\n";</ |
print join("\n", @ages), "\n";</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
Line 2,742: | Line 2,742: | ||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">20</span><span style="color: #0000FF;">}</span> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">20</span><span style="color: #0000FF;">}</span> |
||
<span style="color: #0000FF;">?</span><span style="color: #000000;">countingSort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">))</span> |
<span style="color: #0000FF;">?</span><span style="color: #000000;">countingSort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">))</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,750: | Line 2,750: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php"><?php |
||
function counting_sort(&$arr, $min, $max) |
function counting_sort(&$arr, $min, $max) |
||
Line 2,770: | Line 2,770: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Testing: |
Testing: |
||
< |
<syntaxhighlight lang="php">$ages = array(); |
||
for($i=0; $i < 100; $i++) { |
for($i=0; $i < 100; $i++) { |
||
array_push($ages, rand(0, 140)); |
array_push($ages, rand(0, 140)); |
||
Line 2,783: | Line 2,783: | ||
echo $ages[$i] . "\n"; |
echo $ages[$i] . "\n"; |
||
} |
} |
||
?></ |
?></syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de countingSort (Lst Min Max) |
||
(let Count (need (- Max Min -1) 0) |
(let Count (need (- Max Min -1) 0) |
||
(for N Lst |
(for N Lst |
||
Line 2,795: | Line 2,795: | ||
(do (car C) (link (car I))) ) |
(do (car C) (link (car I))) ) |
||
Count |
Count |
||
(range Min Max) ) ) ) )</ |
(range Min Max) ) ) ) )</syntaxhighlight> |
||
Output: |
Output: |
||
Line 2,802: | Line 2,802: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pl/i">count_sort: procedure (A); |
||
declare A(*) fixed; |
declare A(*) fixed; |
||
declare (min, max) fixed; |
declare (min, max) fixed; |
||
Line 2,830: | Line 2,830: | ||
end; |
end; |
||
end; |
end; |
||
end count_sort;</ |
end count_sort;</syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
function countingSort($array) { |
function countingSort($array) { |
||
$minmax = $array | Measure-Object -Minimum -Maximum |
$minmax = $array | Measure-Object -Minimum -Maximum |
||
Line 2,855: | Line 2,855: | ||
"$array" |
"$array" |
||
"$(countingSort $array)" |
"$(countingSort $array)" |
||
</syntaxhighlight> |
|||
</lang> |
|||
<b>Output:</b> |
<b>Output:</b> |
||
<pre> |
<pre> |
||
Line 2,863: | Line 2,863: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">Procedure Counting_sort(Array data_array(1), min, max) |
||
Define i, j |
Define i, j |
||
Dim c(max - min) |
Dim c(max - min) |
||
Line 2,878: | Line 2,878: | ||
Wend |
Wend |
||
Next |
Next |
||
EndProcedure</ |
EndProcedure</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Follows the spirit of the counting sort but uses Pythons defaultdict(int) to initialize array accesses to zero, and list concatenation: |
Follows the spirit of the counting sort but uses Pythons defaultdict(int) to initialize array accesses to zero, and list concatenation: |
||
< |
<syntaxhighlight lang="python">>>> from collections import defaultdict |
||
>>> def countingSort(array, mn, mx): |
>>> def countingSort(array, mn, mx): |
||
count = defaultdict(int) |
count = defaultdict(int) |
||
Line 2,895: | Line 2,895: | ||
>>> mini,maxi = 1,10 |
>>> mini,maxi = 1,10 |
||
>>> countingSort(data, mini, maxi) == sorted(data) |
>>> countingSort(data, mini, maxi) == sorted(data) |
||
True</ |
True</syntaxhighlight> |
||
Using a list: |
Using a list: |
||
{{works with|Python|2.6}} |
{{works with|Python|2.6}} |
||
< |
<syntaxhighlight lang="python">def countingSort(a, min, max): |
||
cnt = [0] * (max - min + 1) |
cnt = [0] * (max - min + 1) |
||
for x in a: |
for x in a: |
||
Line 2,905: | Line 2,905: | ||
return [x for x, n in enumerate(cnt, start=min) |
return [x for x, n in enumerate(cnt, start=min) |
||
for i in xrange(n)]</ |
for i in xrange(n)]</syntaxhighlight> |
||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery"> [ 2dup peek 1+ |
||
unrot poke ] is [1+] ( [ n --> [ ) |
unrot poke ] is [1+] ( [ n --> [ ) |
||
Line 2,929: | Line 2,929: | ||
dup say "Before: " echo cr |
dup say "Before: " echo cr |
||
10 19 csort |
10 19 csort |
||
say "After: " echo</ |
say "After: " echo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,938: | Line 2,938: | ||
=={{header|R}}== |
=={{header|R}}== |
||
{{trans|Octave}} |
{{trans|Octave}} |
||
< |
<syntaxhighlight lang="r">counting_sort <- function(arr, minval, maxval) { |
||
r <- arr |
r <- arr |
||
z <- 1 |
z <- 1 |
||
Line 2,957: | Line 2,957: | ||
ages <- floor(runif(100, 0, 140+1)) |
ages <- floor(runif(100, 0, 140+1)) |
||
sorted <- counting_sort(ages, 0, 140) |
sorted <- counting_sort(ages, 0, 140) |
||
print(sorted)</ |
print(sorted)</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
Line 2,973: | Line 2,973: | ||
(counting-sort (vector 0 9 3 8 1 -1 1 2 3 7 4) -1 10) |
(counting-sort (vector 0 9 3 8 1 -1 1 2 3 7 4) -1 10) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
< |
<syntaxhighlight lang="racket"> |
||
'#(-1 0 1 1 2 3 3 4 7 8 9) |
'#(-1 0 1 1 2 3 3 4 7 8 9) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{Works with|rakudo|2018.03}} |
{{Works with|rakudo|2018.03}} |
||
<lang |
<syntaxhighlight lang="raku" line>sub counting-sort (@ints) { |
||
my $off = @ints.min; |
my $off = @ints.min; |
||
(my @counts)[$_ - $off]++ for @ints; |
(my @counts)[$_ - $off]++ for @ints; |
||
Line 2,994: | Line 2,994: | ||
say @ages.sort; |
say @ages.sort; |
||
say @ages.&counting-sort.join eq @ages.sort.join ?? 'ok' !! 'not ok';</ |
say @ages.&counting-sort.join eq @ages.sort.join ?? 'ok' !! 'not ok';</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>(5 5 5 7 9 17 19 19 20 21 25 27 28 30 32 34 38 40 41 45 48 49 50 51 53 54 55 56 59 62 65 66 67 69 70 73 74 81 83 85 87 91 91 93 94 96 99 99 100 101) |
<pre>(5 5 5 7 9 17 19 19 20 21 25 27 28 30 32 34 38 40 41 45 48 49 50 51 53 54 55 56 59 62 65 66 67 69 70 73 74 81 83 85 87 91 91 93 94 96 99 99 100 101) |
||
Line 3,006: | Line 3,006: | ||
Negative, zero, and positive integers are supported. |
Negative, zero, and positive integers are supported. |
||
===version 1=== |
===version 1=== |
||
< |
<syntaxhighlight lang="rexx">/*REXX pgm sorts an array of integers (can be negative) using the count─sort algorithm.*/ |
||
$= '1 3 6 2 7 13 20 12 21 11 22 10 23 9 24 8 25 43 62 42 63 41 18 42 17 43 16 44 15 45 14 46 79 113 78 114 77 39 78 38' |
$= '1 3 6 2 7 13 20 12 21 11 22 10 23 9 24 8 25 43 62 42 63 41 18 42 17 43 16 44 15 45 14 46 79 113 78 114 77 39 78 38' |
||
#= words($); w= length(#); !.= 0 /* [↑] a list of some Recaman numbers.*/ |
#= words($); w= length(#); !.= 0 /* [↑] a list of some Recaman numbers.*/ |
||
Line 3,023: | Line 3,023: | ||
return |
return |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
show: do s=1 for #; say right("element",20) right(s,w) arg(1) right(@.s,m); end; return</ |
show: do s=1 for #; say right("element",20) right(s,w) arg(1) right(@.s,m); end; return</syntaxhighlight> |
||
{{out|output|text= when using the default input:}} |
{{out|output|text= when using the default input:}} |
||
Line 3,122: | Line 3,122: | ||
{{trans|PL/I}} |
{{trans|PL/I}} |
||
< |
<syntaxhighlight lang="rexx">/* REXX --------------------------------------------------------------- |
||
* 13.07.2014 Walter Pachl translated from PL/I |
* 13.07.2014 Walter Pachl translated from PL/I |
||
*--------------------------------------------------------------------*/ |
*--------------------------------------------------------------------*/ |
||
Line 3,164: | Line 3,164: | ||
End |
End |
||
Say ol |
Say ol |
||
Return</ |
Return</syntaxhighlight> |
||
'''Output:''' |
'''Output:''' |
||
<pre>before count_sort |
<pre>before count_sort |
||
Line 3,172: | Line 3,172: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
aList = [4, 65, 2, 99, 83, 782, 1] |
aList = [4, 65, 2, 99, 83, 782, 1] |
||
see countingSort(aList, 1, 782) |
see countingSort(aList, 1, 782) |
||
Line 3,195: | Line 3,195: | ||
next |
next |
||
return f |
return f |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">class Array |
||
def counting_sort! |
def counting_sort! |
||
replace counting_sort |
replace counting_sort |
||
Line 3,218: | Line 3,218: | ||
# => [-3, -1, 9, -6, -8, -3, 5, -7, 4, 0, 5, 0, 2, -2, -6, 10, -10, -7, 5, -7] |
# => [-3, -1, 9, -6, -8, -3, 5, -7, 4, 0, 5, 0, 2, -2, -6, 10, -10, -7, 5, -7] |
||
p ary.counting_sort |
p ary.counting_sort |
||
# => [-10, -8, -7, -7, -7, -6, -6, -3, -3, -2, -1, 0, 0, 2, 4, 5, 5, 5, 9, 10]</ |
# => [-10, -8, -7, -7, -7, -6, -6, -3, -3, -2, -1, 0, 0, 2, 4, 5, 5, 5, 9, 10]</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">fn counting_sort( |
||
mut data: Vec<usize>, |
mut data: Vec<usize>, |
||
min: usize, |
min: usize, |
||
Line 3,255: | Line 3,255: | ||
let arr3 = vec![10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]; |
let arr3 = vec![10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]; |
||
println!("{:?}", counting_sort(arr3, 0, 10)); |
println!("{:?}", counting_sort(arr3, 0, 10)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,264: | Line 3,264: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">def countSort(input: List[Int], min: Int, max: Int): List[Int] = |
||
input.foldLeft(Array.fill(max - min + 1)(0)) { (arr, n) => |
input.foldLeft(Array.fill(max - min + 1)(0)) { (arr, n) => |
||
arr(n - min) += 1 |
arr(n - min) += 1 |
||
Line 3,270: | Line 3,270: | ||
}.zipWithIndex.foldLeft(List[Int]()) { |
}.zipWithIndex.foldLeft(List[Int]()) { |
||
case (lst, (cnt, ndx)) => List.fill(cnt)(ndx + min) ::: lst |
case (lst, (cnt, ndx)) => List.fill(cnt)(ndx + min) ::: lst |
||
}.reverse</ |
}.reverse</syntaxhighlight> |
||
It's better (i.e. slightly faster) to reverse the frequencies list before processing it, instead of the whole result |
It's better (i.e. slightly faster) to reverse the frequencies list before processing it, instead of the whole result |
||
< |
<syntaxhighlight lang="scala">def countSort(input: List[Int], min: Int, max: Int): List[Int] = |
||
input.foldLeft(Array.fill(max - min + 1)(0)) { (arr, n) => |
input.foldLeft(Array.fill(max - min + 1)(0)) { (arr, n) => |
||
arr(n - min) += 1 |
arr(n - min) += 1 |
||
Line 3,279: | Line 3,279: | ||
}.zipWithIndex.reverse.foldLeft(List[Int]()) { |
}.zipWithIndex.reverse.foldLeft(List[Int]()) { |
||
case (lst, (cnt, ndx)) => List.fill(cnt)(ndx + min) ::: lst |
case (lst, (cnt, ndx)) => List.fill(cnt)(ndx + min) ::: lst |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">func counting_sort(a, min, max) { |
||
var cnt = ([0] * (max - min + 1)) |
var cnt = ([0] * (max - min + 1)) |
||
a.each {|i| cnt[i-min]++ } |
a.each {|i| cnt[i-min]++ } |
||
Line 3,289: | Line 3,289: | ||
var a = 100.of { 100.irand } |
var a = 100.of { 100.irand } |
||
say counting_sort(a, 0, 100)</ |
say counting_sort(a, 0, 100)</syntaxhighlight> |
||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
< |
<syntaxhighlight lang="slate">s@(Sequence traits) countingSort &min: min &max: max |
||
[| counts index | |
[| counts index | |
||
min `defaultsTo: (s reduce: #min: `er). |
min `defaultsTo: (s reduce: #min: `er). |
||
Line 3,308: | Line 3,308: | ||
]. |
]. |
||
s |
s |
||
].</ |
].</syntaxhighlight> |
||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
{{works with|GNU Smalltalk}} |
{{works with|GNU Smalltalk}} |
||
< |
<syntaxhighlight lang="smalltalk">OrderedCollection extend [ |
||
countingSortWithMin: min andMax: max [ |
countingSortWithMin: min andMax: max [ |
||
|oc z| |
|oc z| |
||
Line 3,329: | Line 3,329: | ||
] |
] |
||
] |
] |
||
].</ |
].</syntaxhighlight> |
||
Testing: |
Testing: |
||
< |
<syntaxhighlight lang="smalltalk">|ages| |
||
ages := OrderedCollection new. |
ages := OrderedCollection new. |
||
Line 3,342: | Line 3,342: | ||
ages countingSortWithMin: 0 andMax: 140. |
ages countingSortWithMin: 0 andMax: 140. |
||
ages printNl.</ |
ages printNl.</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{works with|Tcl|8.5}} |
{{works with|Tcl|8.5}} |
||
< |
<syntaxhighlight lang="tcl">proc countingsort {a {min ""} {max ""}} { |
||
# If either of min or max weren't given, compute them now |
# If either of min or max weren't given, compute them now |
||
if {$min eq ""} { |
if {$min eq ""} { |
||
Line 3,386: | Line 3,386: | ||
for {set i 0} {$i < 50} {incr i} {lappend a [expr {1+ int(rand()*10)}]} |
for {set i 0} {$i < 50} {incr i} {lappend a [expr {1+ int(rand()*10)}]} |
||
puts $a |
puts $a |
||
puts [countingsort $a]</ |
puts [countingsort $a]</syntaxhighlight> |
||
<pre>9 7 10 2 9 7 4 3 10 2 7 10 2 1 3 8 7 3 9 5 8 5 1 6 3 7 5 4 6 9 9 6 6 10 2 4 5 2 8 2 2 5 2 9 3 3 5 7 8 4 |
<pre>9 7 10 2 9 7 4 3 10 2 7 10 2 1 3 8 7 3 9 5 8 5 1 6 3 7 5 4 6 9 9 6 6 10 2 4 5 2 8 2 2 5 2 9 3 3 5 7 8 4 |
||
1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 5 5 5 5 5 5 6 6 6 6 7 7 7 7 7 7 8 8 8 8 9 9 9 9 9 9 10 10 10 10 |
1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 5 5 5 5 5 5 6 6 6 6 7 7 7 7 7 7 8 8 8 8 9 9 9 9 9 9 10 10 10 10 |
||
Line 3,392: | Line 3,392: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
{{trans|Phix}}< |
{{trans|Phix}}<syntaxhighlight lang="vb">Option Base 1 |
||
Private Function countingSort(array_ As Variant, mina As Long, maxa As Long) As Variant |
Private Function countingSort(array_ As Variant, mina As Long, maxa As Long) As Variant |
||
Dim count() As Integer |
Dim count() As Integer |
||
Line 3,412: | Line 3,412: | ||
s = [{5, 3, 1, 7, 4, 1, 1, 20}] |
s = [{5, 3, 1, 7, 4, 1, 1, 20}] |
||
Debug.Print Join(countingSort(s, WorksheetFunction.Min(s), WorksheetFunction.Max(s)), ", ") |
Debug.Print Join(countingSort(s, WorksheetFunction.Min(s), WorksheetFunction.Max(s)), ", ") |
||
End Sub</ |
End Sub</syntaxhighlight>{{out}} |
||
<pre>1, 1, 1, 3, 4, 5, 7, 20</pre> |
<pre>1, 1, 1, 3, 4, 5, 7, 20</pre> |
||
Line 3,419: | Line 3,419: | ||
=====Implementation===== |
=====Implementation===== |
||
< |
<syntaxhighlight lang="vb">function findMax( a ) |
||
dim num |
dim num |
||
dim max |
dim max |
||
Line 3,460: | Line 3,460: | ||
next |
next |
||
countingSort = a |
countingSort = a |
||
end function</ |
end function</syntaxhighlight> |
||
=====Invocation===== |
=====Invocation===== |
||
< |
<syntaxhighlight lang="vb">dim a |
||
a = array(300, 1, -2, 3, -4, 5, -6, 7, -8, 100, 11 ) |
a = array(300, 1, -2, 3, -4, 5, -6, 7, -8, 100, 11 ) |
||
wscript.echo join( a, ", " ) |
wscript.echo join( a, ", " ) |
||
countingSort a |
countingSort a |
||
wscript.echo join( a, ", " )</ |
wscript.echo join( a, ", " )</syntaxhighlight> |
||
=====Output===== |
=====Output===== |
||
Line 3,476: | Line 3,476: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
< |
<syntaxhighlight lang="vlang">fn counting_sort(mut arr []int, min int, max int) { |
||
println('Input: ' + arr.str()) |
println('Input: ' + arr.str()) |
||
mut count := [0].repeat(max - min + 1) |
mut count := [0].repeat(max - min + 1) |
||
Line 3,499: | Line 3,499: | ||
mut arr := [6, 2, 1, 7, 6, 8] |
mut arr := [6, 2, 1, 7, 6, 8] |
||
counting_sort(mut arr, 1, 8) |
counting_sort(mut arr, 1, 8) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Input: [6, 2, 1, 7, 6, 8] |
<pre>Input: [6, 2, 1, 7, 6, 8] |
||
Line 3,505: | Line 3,505: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
< |
<syntaxhighlight lang="ecmascript">var countingSort = Fn.new { |a, min, max| |
||
var count = List.filled(max - min + 1, 0) |
var count = List.filled(max - min + 1, 0) |
||
for (n in a) count[n - min] = count[n - min] + 1 |
for (n in a) count[n - min] = count[n - min] + 1 |
||
Line 3,523: | Line 3,523: | ||
var max = a.reduce { |max, i| (i > max) ? i : max } |
var max = a.reduce { |max, i| (i > max) ? i : max } |
||
countingSort.call(a, min, max) |
countingSort.call(a, min, max) |
||
System.print("Sorted : %(a)")</ |
System.print("Sorted : %(a)")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,532: | Line 3,532: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; |
||
proc CountingSort(Array, Min, Max, Size); \Sort Array |
proc CountingSort(Array, Min, Max, Size); \Sort Array |
||
Line 3,554: | Line 3,554: | ||
CountingSort(A, -5, 9, 10); |
CountingSort(A, -5, 9, 10); |
||
for I:= 0 to 10-1 do [IntOut(0, A(I)); ChOut(0, ^ )]; |
for I:= 0 to 10-1 do [IntOut(0, A(I)); ChOut(0, ^ )]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,562: | Line 3,562: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn countingSort(array, min, max){ // modifies array |
||
count:=(max - min + 1).pump(List().write,0); // array of (max - min + 1) zeros |
count:=(max - min + 1).pump(List().write,0); // array of (max - min + 1) zeros |
||
foreach number in (array){ |
foreach number in (array){ |
||
Line 3,572: | Line 3,572: | ||
} |
} |
||
array |
array |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">array:=List(4, 65, 2, -31, 0, 99, 2, 83, 182, 1); |
||
countingSort(array,(0).min(array), (0).max(array)).println();</ |
countingSort(array,(0).min(array), (0).max(array)).println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>L(-31,0,1,2,2,4,65,83,99,182)</pre> |
<pre>L(-31,0,1,2,2,4,65,83,99,182)</pre> |