September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -24,5 +24,6 @@ Pseudocode:
|
|||
|
||||
The ''min'' and ''max'' can be computed apart, or be known ''a priori''.
|
||||
|
||||
'''Note''': we know that, given an array of integers, its maximum and minimum values can be always found; but if we imagine the worst case for an array of 32 bit integers, we see that in order to hold the counts, we need an array of 2<sup>32</sup> elements, i.e., we need, to hold a count value up to 2<sup>32</sup>-1, more or less 4 Gbytes. So the counting sort is more practical when the range is (very) limited and minimum and maximum values are known ''a priori''. (Anyway sparse arrays may limit the impact of the memory usage)
|
||||
|
||||
'''Note''': we know that, given an array of integers, its maximum and minimum values can be always found; but if we imagine the worst case for an array that can hold up to 32 bit integers, we see that in order to hold the counts, an array of up to '''2<sup>32</sup>''' elements may be needed. I.E.: we need to hold a count value up to '''2<sup>32</sup>-1''', which is a little over 4.2 Gbytes. So the counting sort is more practical when the range is (very) limited, and minimum and maximum values are known ''a priori''. (The use of ''sparse arrays'' minimizes the impact of the memory usage, as well as removing the need of having to know the minimum and maximum values ''a priori''.)
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -20,16 +20,16 @@ void counting_sort_mm(int *array, int n, int min, int max)
|
|||
free(count);
|
||||
}
|
||||
|
||||
void counting_sort(int *array, int n)
|
||||
void min_max(int *array, int n, int *min, int *max)
|
||||
{
|
||||
int i, min, max;
|
||||
int i;
|
||||
|
||||
min = max = array[0];
|
||||
*min = *max = array[0];
|
||||
for(i=1; i < n; i++) {
|
||||
if ( array[i] < min ) {
|
||||
min = array[i];
|
||||
} else if ( array[i] > max ) {
|
||||
max = array[i];
|
||||
if ( array[i] < *min ) {
|
||||
*min = array[i];
|
||||
} else if ( array[i] > *max ) {
|
||||
*max = array[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,37 +1,37 @@
|
|||
import extensions.
|
||||
import system'routines.
|
||||
import extensions;
|
||||
import system'routines;
|
||||
|
||||
extension $op
|
||||
extension op
|
||||
{
|
||||
countingSort
|
||||
= self clone; countingSort(self minimalMember, self maximalMember).
|
||||
countingSort()
|
||||
= self.clone().countingSort(self.MinimalMember, self.MaximalMember);
|
||||
|
||||
countingSort int:min int:max
|
||||
[
|
||||
array<int> count := int<>(max - min + 1).
|
||||
int z := 0.
|
||||
countingSort(int min, int max)
|
||||
{
|
||||
int[] count := new int[](max - min + 1);
|
||||
int z := 0;
|
||||
|
||||
count populate(:i)<int>(0).
|
||||
count.populate:(int i => 0);
|
||||
|
||||
0 till(self length) do(:i) [ count[self[i] - min] += 1 ].
|
||||
for(int i := 0, i < self.Length, i += 1) { count[self[i] - min] := count[self[i] - min] + 1 };
|
||||
|
||||
min to:max do(:i)
|
||||
[
|
||||
for(int i := min, i <= max, i += 1)
|
||||
{
|
||||
while (count[i - min] > 0)
|
||||
[
|
||||
self[z] := i.
|
||||
z += 1.
|
||||
{
|
||||
self[z] := i;
|
||||
z += 1;
|
||||
|
||||
count[i - min] -= 1.
|
||||
]
|
||||
]
|
||||
]
|
||||
count[i - min] := count[i - min] - 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
program =
|
||||
[
|
||||
var list := 0 to:10 repeat(:i)(randomGenerator eval(10)); toArray.
|
||||
public program()
|
||||
{
|
||||
var list := new Range(0, 10).selectBy:(i => randomGenerator.eval(10)).toArray();
|
||||
|
||||
console printLine("before:", list).
|
||||
console printLine("after :", list countingSort).
|
||||
].
|
||||
console.printLine("before:", list.asEnumerable());
|
||||
console.printLine("after :", list.countingSort().asEnumerable())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,8 +21,8 @@ contains
|
|||
integer, dimension(tmin:tmax) :: cnt
|
||||
integer :: i, z
|
||||
|
||||
cnt = 0 ! Initialize to zero to prevent false counts
|
||||
FORALL (I=1:Z) ! Not sure that this gives any benefit over a DO loop.
|
||||
cnt = 0 ! Initialize to zero to prevent false counts
|
||||
FORALL (I=1:size(array)) ! Not sure that this gives any benefit over a DO loop.
|
||||
cnt(array(i)) = cnt(array(i))+1
|
||||
END FORALL
|
||||
!
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
/*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
|
||||
#=words($); w=length(#) /* [↑] a list of some Recaman numbers.*/
|
||||
m=0; _.=0 /*M: max width of any number in @. */
|
||||
do i=1 for #; z=word($, i); @.i=z; m=max(m, length(z)) /*get from $ list. */
|
||||
if i==1 then do; LO=z; HI=z; end /*assume z: LO & HI*/
|
||||
_.z= _.z + 1; LO=min(LO, z); HI=max(HI, z) /*find the LO & HI.*/
|
||||
#= words($); w= length(#); _.= 0 /* [↑] a list of some Recaman numbers.*/
|
||||
m= 0; LO= word($, 1); HI= LO /*M: max width of any number in @. */
|
||||
do i=1 for #; z= word($, i); @.i= z; m= max(m, length(z)) /*get from $ list. */
|
||||
_.z= _.z + 1; LO= min(LO, z); HI= max(HI, z) /*find the LO & HI.*/
|
||||
end /*i*/
|
||||
/*W: max index width for the @. array*/
|
||||
call show 'before sort: ' /*show the before array elements. */
|
||||
|
|
@ -13,9 +12,8 @@ call countSort # /*sort a number of entries of @
|
|||
call show ' after sort: ' /*show the after array elements. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
countSort: parse arg N; x=1
|
||||
do k=LO to HI; do x=x for _.k; @.x=k; end /*x*/
|
||||
end /*k*/
|
||||
countSort: parse arg N; x= 1; do k=LO to HI; do x=x for _.k; @.x= k; end /*x*/
|
||||
end /*k*/
|
||||
return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
show: do s=1 for #; say right("element",20) right(s,w) arg(1) right(@.s,m); end; return
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
Option Base 1
|
||||
Private Function countingSort(array_ As Variant, mina As Long, maxa As Long) As Variant
|
||||
Dim count() As Integer
|
||||
ReDim count(maxa - mina + 1)
|
||||
For i = 1 To UBound(array_)
|
||||
count(array_(i) - mina + 1) = count(array_(i) - mina + 1) + 1
|
||||
Next i
|
||||
Dim z As Integer: z = 1
|
||||
For i = mina To maxa
|
||||
For j = 1 To count(i - mina + 1)
|
||||
array_(z) = i
|
||||
z = z + 1
|
||||
Next j
|
||||
Next i
|
||||
countingSort = array_
|
||||
End Function
|
||||
|
||||
Public Sub main()
|
||||
s = [{5, 3, 1, 7, 4, 1, 1, 20}]
|
||||
Debug.Print Join(countingSort(s, WorksheetFunction.Min(s), WorksheetFunction.Max(s)), ", ")
|
||||
End Sub
|
||||
Loading…
Add table
Add a link
Reference in a new issue