Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -48,7 +48,7 @@ int main( int argc, char* argv[] )
srand( time( NULL ) ); int arr[MAX];
for( int i = 0; i < MAX; i++ )
arr[i] = rand() % 140 - rand() % 40 + 1;
for( int i = 0; i < MAX; i++ )
cout << arr[i] << ", ";
cout << endl << endl;

View file

@ -0,0 +1,30 @@
#include <algorithm>
#include <iterator>
#include <iostream>
#include <vector>
template<typename ForwardIterator> void counting_sort(ForwardIterator begin,
ForwardIterator end) {
auto min_max = std::minmax_element(begin, end);
if (min_max.first == min_max.second) { // empty range
return;
}
auto min = *min_max.first;
auto max = *min_max.second;
std::vector<unsigned> count((max - min) + 1, 0u);
for (auto i = begin; i != end; ++i) {
++count[*i - min];
}
for (auto i = min; i <= max; ++i) {
for (auto j = 0; j < count[i - min]; ++j) {
*begin++ = i;
}
}
}
int main() {
int a[] = {100, 2, 56, 200, -52, 3, 99, 33, 177, -199};
counting_sort(std::begin(a), std::end(a));
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
}

View file

@ -0,0 +1,34 @@
class
COUNTING_SORT
feature
sort(ar: ARRAY[INTEGER]; min, max: INTEGER): ARRAY[INTEGER]
local
count: ARRAY[INTEGER]
i, j, z: INTEGER
do
create count.make_filled (0, 0, max-min)
from
i:= 0
until
i= ar.count
loop
count[ar[i]-min]:= count[ar[i]-min]+1
i:= i+1
end
across count as c loop io.put_string (c.item.out + "%T") end
z:= 0
from i:= min
until i>max
loop
from j:= 0
until j= count[i-min]
loop
ar[z]:=i
z:= z+1
j:= j+1
end
i:= i+1
end
Result:= ar
end
end

View file

@ -0,0 +1,24 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
feature
make
do
create test.make_filled(0,0,5)
test[0]:=-7
test[1]:=4
test[2]:=2
test[3]:=6
test[4]:=1
test[5]:=3
across test as t loop io.put_string (t.item.out + "%T") end
create count
test:=count.sort (test, -7, 6)
across test as ar loop io.put_string (ar.item.out+"%T") end
end
count: COUNTING_SORT
test: ARRAY[INTEGER]
end

View file

@ -0,0 +1,23 @@
fn countingSort arr =
(
if arr.count < 2 do return arr
local minVal = amin arr
local maxVal = amax arr
local count = for i in 1 to (maxVal-minVal+1) collect 0
for i in arr do
(
count[i-minVal+1] = count[i-minVal+1] + 1
)
local z = 1
for i = minVal to maxVal do
(
while (count[i-minVal+1]>0) do
(
arr[z] = i
z += 1
count[i-minVal+1] = count[i-minVal+1] - 1
)
)
return arr
)

View file

@ -0,0 +1,4 @@
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)
countingSort a
#(1, 6, 7, 7, 11, 11, 15, 16, 16, 17, 22, 24, 25, 27, 28)

View file

@ -0,0 +1,32 @@
/*REXX program sorts an array using the count-sort algorithm. */
call gen@ /*generate the array elements. */
call show@ 'before sort' /*show the before array elements.*/
call countSort # /*sort N entries of the @. array.*/
call show@ ' after sort' /*show the after array elements.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────COUNTSORT subroutine────────────────*/
countSort: procedure expose @.; parse arg N; h=@.1; L=h
do i=2 to N; L=min(L,@.i); h=max(h,@.i); end /*i*/
_.=0; do j=1 for N; x=@.j; _.x=_.x+1; end /*j*/
x=1; do k=L to h; if _.k\==0 then do x=x for _.k
@.x=k
end /*x*/
end /*k*/
return
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
gen@: $=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($)
do j=1 for # /* [↓] assign 40 Recaman numbers.*/
@.j=word($,j) /*assign a Recaman # from a list.*/
end /*j*/
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@: do s=1 for #
say left('',9) "element" right(s,length(#)) arg(1)': ' @.s
end /*s*/
say copies('',50) /*show a pretty separator line. */
return

View file

@ -0,0 +1,43 @@
/* REXX ---------------------------------------------------------------
* 13.07.2014 Walter Pachl translated from PL/I
*--------------------------------------------------------------------*/
alist='999 888 777 1 5 13 15 17 19 21 5'
Parse Var alist lo hi .
Do i=1 By 1 While alist<>''
Parse Var alist a.i alist;
lo=min(lo,a.i)
hi=max(hi,a.i)
End
a.0=i-1
Call show 'before count_sort'
Call count_sort
Call show 'after count_sort'
Exit
count_sort: procedure Expose a. lo hi
t.=0
do i=1 to a.0
j=a.i
t.j=t.j+1
end
k=1
do i=lo to hi
if t.i<>0 then Do
do j=1 to t.i
a.k=i
k=k+1
end
end
end
Return
show: Procedure Expose a.
Parse Arg head
Say head
ol=''
Do i=1 To a.0
ol=ol right(a.i,3)
End
Say ol
Return

View file

@ -1,41 +0,0 @@
/*REXX program sorts an array using the count-sort method. */
call gen@ /*generate the array elements. */
call show@ 'before sort' /*show the before array elements.*/
call countSort N /*sort N entries of the @. array.*/
call show@ ' after sort' /*show the after array elements.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────countSort subroutine────────────────*/
countSort: procedure expose @.; parse arg N; h=@.1; L=h
do i=2 to N; L=min(L,@.i); h=max(h,@.i)
end /*i*/
_.=0; do j=1 for N; x=@.j; _.x=_.x+1
end /*j*/
#=1; do k=L to h; if _.k\==0 then do #=# for _.k
@.#=k
end /*#*/
end /*k*/
return
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
gen@: @.= /*assign 40 Recaman numbers. */
@.1 = 1 ; @.9 = 21 ; @.17= 25 ; @.25= 17 ; @.33= 79
@.2 = 3 ; @.10= 11 ; @.18= 43 ; @.26= 43 ; @.34= 113
@.3 = 6 ; @.11= 22 ; @.19= 62 ; @.27= 16 ; @.35= 78
@.4 = 2 ; @.12= 10 ; @.20= 42 ; @.28= 44 ; @.36= 114
@.5 = 7 ; @.13= 23 ; @.21= 63 ; @.29= 15 ; @.37= 77
@.6 = 13 ; @.14= 9 ; @.22= 41 ; @.30= 45 ; @.38= 39
@.7 = 20 ; @.15= 24 ; @.23= 18 ; @.31= 14 ; @.39= 78
@.8 = 12 ; @.16= 8 ; @.24= 42 ; @.32= 46 ; @.40= 38
do N=1 while @.N\==''; end /*determine the number of entries*/
N=N-1 /*adjust highItem slightly. */
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@: widthH=length(N) /*max width of any element number*/
pad=left('',9); do s=1 for N
say pad 'element' right(s,widthH) arg(1)": " @.s
end /*s*/
say copies('',40) /*show a pretty separator line. */
return