Sorting algorithms/Counting sort: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 125:
 
0 1 2 3 3 4 4 5 6 7 8 9 9 10 11 12 15 18 18 19 21 21 22 27 33 35 36 38 38 38 38 39 40 40 41 43 44 53 54 55 57 57 58 59 59 60 60 60 60 61 62 64 65 66 67 68 70 71 78 79 82 83 84 84 87 87 88 88 88 89 89 92 93 93 97 98 99 99 100 107 109 114 115 115 118 122 126 127 127 129 129 130 131 133 134 136 136 137 139 139
 
=={{header|AutoHotkey}}==
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276465.html#276465 forum]
Line 140 ⟶ 141:
}
Return SubStr(t,2)
}</lang>
 
=={{header|BASIC256}}==
Line 261 ⟶ 262:
for(i=0; i < N; i++) printf("%d\n", ages[i]);
return EXIT_SUCCESS;
}</lang>
 
=={{header|C sharp|C#}}==
<lang csharp>using System;
using System.Linq;
 
namespace CountingSort
{
class Program
{
static void Main(string[] args)
{
Random rand = new Random(); // Just for creating a test array
int[] arr = new int[100]; // of random numbers
for (int i = 0; i < 100; i++) { arr[i] = rand.Next(0, 100); } // ...
 
int[] newarr = countingSort(arr, arr.Min(), arr.Max());
}
 
private static int[] countingSort(int[] arr, int min, int max)
{
int[] count = new int[max - min + 1];
int z = 0;
 
for (int i = 0; i < count.Length; i++) { count[i] = 0; }
for (int i = 0; i < arr.Length; i++) { count[arr[i] - min]++; }
 
for (int i = min; i <= max; i++)
{
while (count[i - min]-- > 0)
{
arr[z] = i;
z++;
}
}
return arr;
}
}
}</lang>
 
Line 376 ⟶ 415:
-199 -52 2 3 33 56 99 100 177 200
</pre>
 
=={{header|C sharp|C#}}==
<lang csharp>using System;
using System.Linq;
 
namespace CountingSort
{
class Program
{
static void Main(string[] args)
{
Random rand = new Random(); // Just for creating a test array
int[] arr = new int[100]; // of random numbers
for (int i = 0; i < 100; i++) { arr[i] = rand.Next(0, 100); } // ...
 
int[] newarr = countingSort(arr, arr.Min(), arr.Max());
}
 
private static int[] countingSort(int[] arr, int min, int max)
{
int[] count = new int[max - min + 1];
int z = 0;
 
for (int i = 0; i < count.Length; i++) { count[i] = 0; }
for (int i = 0; i < arr.Length; i++) { count[arr[i] - min]++; }
 
for (int i = min; i <= max; i++)
{
while (count[i - min]-- > 0)
{
arr[z] = i;
z++;
}
}
return arr;
}
}
}</lang>
 
=={{header|Common Lisp}}==
Line 483 ⟶ 484:
? arr
# value: [0, 0, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 34, 34, 34, 37, 56, 67, 76, 78, 234, 435, 435, 453, 467, 534, 634, 735].diverge()</pre>
 
 
=={{header|Eiffel}}==
Line 609:
-7 1 2 3 4 6
</pre>
 
=={{header|Elena}}==
ELENA 4.x :
Line 986 ⟶ 987:
Sorted Integers: [-19,-1,0,1,2,4,5,5,10,23]
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
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).
 
<lang Icon>procedure main() #: demonstrate various ways to sort a list and string
write("Sorting Demo using ",image(countingsort))
writes(" on list : ")
writex(UL)
displaysort(countingsort,copy(UL))
end
 
procedure countingsort(X) #: return sorted list (integers only)
local T,lower,upper
 
T := table(0) # hash table as sparse array
lower := upper := X[1]
 
every x := !X do {
if not ( integer(x) = x ) then runerr(x,101) # must be integer
lower >:= x # minimum
upper <:= x # maximum
T[x] +:= 1 # record x's and duplicates
}
 
every put(X := [],( 1 to T[i := lower to upper], i) ) # reconstitute with correct order and count
return X
end</lang>
 
Note: This example relies on [[Sorting_algorithms/Bubble_sort#Icon| the supporting procedures 'display sort', and 'writex' from Bubble Sort]].
 
Sample output:<pre>Sorting Demo using procedure countingsort
on list : [ 3 14 1 5 9 2 6 3 ]
with op = &null: [ 1 2 3 3 5 6 9 14 ] (0 ms)</pre>
 
=={{header|Io}}==
Line 1,038 ⟶ 1,072:
l := list(2, 3, -4, 5, 1)
l countingSortInPlace println # ==> list(-4, 1, 2, 3, 5)</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
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).
 
<lang Icon>procedure main() #: demonstrate various ways to sort a list and string
write("Sorting Demo using ",image(countingsort))
writes(" on list : ")
writex(UL)
displaysort(countingsort,copy(UL))
end
 
procedure countingsort(X) #: return sorted list (integers only)
local T,lower,upper
 
T := table(0) # hash table as sparse array
lower := upper := X[1]
 
every x := !X do {
if not ( integer(x) = x ) then runerr(x,101) # must be integer
lower >:= x # minimum
upper <:= x # maximum
T[x] +:= 1 # record x's and duplicates
}
 
every put(X := [],( 1 to T[i := lower to upper], i) ) # reconstitute with correct order and count
return X
end</lang>
 
Note: This example relies on [[Sorting_algorithms/Bubble_sort#Icon| the supporting procedures 'display sort', and 'writex' from Bubble Sort]].
 
Sample output:<pre>Sorting Demo using procedure countingsort
on list : [ 3 14 1 5 9 2 6 3 ]
with op = &null: [ 1 2 3 3 5 6 9 14 ] (0 ms)</pre>
 
=={{header|IS-BASIC}}==
Line 1,462 ⟶ 1,463:
 
1 2 3 4 5 6</lang>
 
=={{header|MAXScript}}==
<lang MAXScript>
Line 1,894 ⟶ 1,896:
counting_sort(\@ages, 0, 140);
print join("\n", @ages), "\n";</lang>
 
=={{header|Perl 6}}==
{{Works with|rakudo|2018.03}}
<lang perl6>sub counting-sort (@ints) {
my $off = @ints.min;
(my @counts)[$_ - $off]++ for @ints;
flat @counts.kv.map: { ($^k + $off) xx ($^v // 0) }
}
 
# Testing:
constant @age-range = 2 .. 102;
my @ages = @age-range.roll(50);
say @ages.&counting-sort;
say @ages.sort;
 
say @ages.&counting-sort.join eq @ages.sort.join ?? 'ok' !! 'not ok';</lang>
{{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)
(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)
ok
</pre>
 
=={{header|Phix}}==
Line 2,140 ⟶ 2,121:
'#(-1 0 1 1 2 3 3 4 7 8 9)
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2018.03}}
<lang perl6>sub counting-sort (@ints) {
my $off = @ints.min;
(my @counts)[$_ - $off]++ for @ints;
flat @counts.kv.map: { ($^k + $off) xx ($^v // 0) }
}
 
# Testing:
constant @age-range = 2 .. 102;
my @ages = @age-range.roll(50);
say @ages.&counting-sort;
say @ages.sort;
 
say @ages.&counting-sort.join eq @ages.sort.join ?? 'ok' !! 'not ok';</lang>
{{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)
(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)
ok
</pre>
 
=={{header|REXX}}==
Line 2,547 ⟶ 2,550:
End Sub</lang>{{out}}
<pre>1, 1, 1, 3, 4, 5, 7, 20</pre>
 
=={{header|VBScript}}==
All my other sort demos just pass in the array, thus the findMax and findMin
10,333

edits