Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -0,0 +1,52 @@
|
|||
#--- Swap function ---#
|
||||
PROC swap = (REF []INT array, INT first, INT second) VOID:
|
||||
(
|
||||
INT temp := array[first];
|
||||
array[first] := array[second];
|
||||
array[second]:= temp
|
||||
);
|
||||
|
||||
#--- Heap sort Move Down ---#
|
||||
PROC heapmove = (REF []INT array, INT i, INT last) VOID:
|
||||
(
|
||||
INT index := i;
|
||||
INT larger := (index*2);
|
||||
|
||||
WHILE larger <= last DO
|
||||
IF larger < last THEN IF array[larger] < array[larger+1] THEN
|
||||
larger +:= 1
|
||||
FI FI;
|
||||
IF array[index] < array[larger] THEN
|
||||
swap(array, index, larger)
|
||||
FI;
|
||||
index := larger;
|
||||
larger := (index*2)
|
||||
OD
|
||||
);
|
||||
|
||||
#--- Heap sort ---#
|
||||
PROC heapsort = (REF []INT array) VOID:
|
||||
(
|
||||
FOR i FROM ENTIER((UPB array) / 2) BY -1 WHILE
|
||||
heapmove(array, i, UPB array);
|
||||
i > 1 DO SKIP OD;
|
||||
|
||||
FOR i FROM UPB array BY -1 WHILE
|
||||
swap(array, 1, i);
|
||||
heapmove(array, 1, i-1);
|
||||
i > 1 DO SKIP OD
|
||||
);
|
||||
#***************************************************************#
|
||||
main:
|
||||
(
|
||||
[10]INT a;
|
||||
FOR i FROM 1 TO UPB a DO
|
||||
a[i] := ROUND(random*100)
|
||||
OD;
|
||||
|
||||
print(("Before:", a));
|
||||
print((newline, newline));
|
||||
heapsort(a);
|
||||
print(("After: ", a))
|
||||
|
||||
)
|
||||
|
|
@ -1,21 +1,16 @@
|
|||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <iostream>
|
||||
#include <algorithm> // for std::make_heap, std::sort_heap
|
||||
|
||||
template <typename Iterator>
|
||||
void heapsort(Iterator begin, Iterator end)
|
||||
{
|
||||
std::make_heap(begin, end);
|
||||
std::sort_heap(begin, end);
|
||||
template<typename RandomAccessIterator>
|
||||
void heap_sort(RandomAccessIterator begin, RandomAccessIterator end) {
|
||||
std::make_heap(begin, end);
|
||||
std::sort_heap(begin, end);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
double valsToSort[] = {
|
||||
1.4, 50.2, 5.11, -1.55, 301.521, 0.3301, 40.17,
|
||||
-18.0, 88.1, 30.44, -37.2, 3012.0, 49.2};
|
||||
const int VSIZE = sizeof(valsToSort)/sizeof(*valsToSort);
|
||||
|
||||
heapsort(valsToSort, valsToSort+VSIZE);
|
||||
for (int ix=0; ix<VSIZE; ix++) std::cout << valsToSort[ix] << std::endl;
|
||||
return 0;
|
||||
int main() {
|
||||
int a[] = {100, 2, 56, 200, -52, 3, 99, 33, 177, -199};
|
||||
heap_sort(std::begin(a), std::end(a));
|
||||
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " "));
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,47 @@
|
|||
#include <iostream>
|
||||
#include <algorithm> // for std::make_heap, std::pop_heap
|
||||
#include <vector>
|
||||
|
||||
template <typename Iterator>
|
||||
void heapsort(Iterator begin, Iterator end)
|
||||
{
|
||||
std::make_heap(begin, end);
|
||||
while (begin != end)
|
||||
std::pop_heap(begin, end--);
|
||||
using namespace std;
|
||||
|
||||
void shift_down(vector<int>& heap,int i, int max) {
|
||||
int i_big, c1, c2;
|
||||
while(i < max) {
|
||||
i_big = i;
|
||||
c1 = (2*i) + 1;
|
||||
c2 = c1 + 1;
|
||||
if( c1<max && heap[c1]>heap[i_big] )
|
||||
i_big = c1;
|
||||
if( c2<max && heap[c2]>heap[i_big] )
|
||||
i_big = c2;
|
||||
if(i_big == i) return;
|
||||
swap(heap[i],heap[i_big]);
|
||||
i = i_big;
|
||||
}
|
||||
}
|
||||
|
||||
void to_heap(vector<int>& arr) {
|
||||
int i = (arr.size()/2) - 1;
|
||||
while(i >= 0) {
|
||||
shift_down(arr, i, arr.size());
|
||||
--i;
|
||||
}
|
||||
}
|
||||
|
||||
void heap_sort(vector<int>& arr) {
|
||||
to_heap(arr);
|
||||
int end = arr.size() - 1;
|
||||
while (end > 0) {
|
||||
swap(arr[0], arr[end]);
|
||||
shift_down(arr, 0, end);
|
||||
--end;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
vector<int> data = {
|
||||
12, 11, 15, 10, 9, 1, 2,
|
||||
3, 13, 14, 4, 5, 6, 7, 8
|
||||
};
|
||||
heap_sort(data);
|
||||
for(int i : data) cout << i << " ";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +0,0 @@
|
|||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <iostream>
|
||||
|
||||
template<typename RandomAccessIterator>
|
||||
void heap_sort(RandomAccessIterator begin, RandomAccessIterator end) {
|
||||
std::make_heap(begin, end);
|
||||
std::sort_heap(begin, end);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int a[] = {100, 2, 56, 200, -52, 3, 99, 33, 177, -199};
|
||||
heap_sort(std::begin(a), std::end(a));
|
||||
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " "));
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
class
|
||||
HEAPSORT
|
||||
|
||||
feature
|
||||
|
||||
sort_array (ar: ARRAY [INTEGER])
|
||||
-- Sorts array 'ar' in ascending order.
|
||||
require
|
||||
not_empty: ar.count > 0
|
||||
local
|
||||
i, j, r, l, m, n: INTEGER
|
||||
sorted: BOOLEAN
|
||||
do
|
||||
n := ar.count
|
||||
j := 0
|
||||
i := 0
|
||||
m := 0
|
||||
r := n
|
||||
l := (n // 2)+1
|
||||
from
|
||||
until
|
||||
sorted
|
||||
loop
|
||||
if l > 1 then
|
||||
l := l - 1
|
||||
m := ar[l]
|
||||
else
|
||||
m := ar[r]
|
||||
ar[r] := ar[1]
|
||||
r := r - 1
|
||||
if r = 1 then
|
||||
ar[1]:=m
|
||||
sorted := True
|
||||
end
|
||||
end
|
||||
if not sorted then
|
||||
i := l
|
||||
j := l * 2
|
||||
from
|
||||
until
|
||||
j > r
|
||||
loop
|
||||
if (j < r) and (ar[j] < ar[j + 1]) then
|
||||
j := j + 1
|
||||
end
|
||||
if m < ar[j] then
|
||||
ar[i]:= ar[j]
|
||||
i := j
|
||||
j := j + i
|
||||
else
|
||||
j := r + 1
|
||||
end
|
||||
end
|
||||
ar[i]:= m
|
||||
end
|
||||
end
|
||||
ensure
|
||||
sorted: is_sorted(ar)
|
||||
end
|
||||
|
||||
feature{NONE}
|
||||
|
||||
is_sorted (ar: ARRAY [INTEGER]): BOOLEAN
|
||||
--- Is 'ar' sorted in ascending order?
|
||||
local
|
||||
i: INTEGER
|
||||
do
|
||||
Result := True
|
||||
from
|
||||
i := ar.lower
|
||||
until
|
||||
i >= ar.upper
|
||||
loop
|
||||
if ar [i] > ar [i + 1] then
|
||||
Result := False
|
||||
end
|
||||
i := i + 1
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
class
|
||||
APPLICATION
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature
|
||||
|
||||
make
|
||||
local
|
||||
test: ARRAY [INTEGER]
|
||||
do
|
||||
create test.make_empty
|
||||
test := <<5, 91, 13, 99,7, 35>>
|
||||
io.put_string ("Unsorted: ")
|
||||
across
|
||||
test as t
|
||||
loop
|
||||
io.put_string (t.item.out + " ")
|
||||
end
|
||||
io.new_line
|
||||
create heap_sort
|
||||
heap_sort.sort_array (test)
|
||||
io.put_string ("Sorted: ")
|
||||
across
|
||||
test as t
|
||||
loop
|
||||
io.put_string (t.item.out + " ")
|
||||
end
|
||||
end
|
||||
|
||||
heap_sort: HEAPSORT
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
function heapsort($a, $count) {
|
||||
$a = heapify $a $count
|
||||
$end = $count - 1
|
||||
while( $end -gt 0) {
|
||||
$a[$end], $a[0] = $a[0], $a[$end]
|
||||
$end--
|
||||
$a = siftDown $a 0 $end
|
||||
}
|
||||
$a
|
||||
}
|
||||
function heapify($a, $count) {
|
||||
$start = [Math]::Floor(($count - 2) / 2)
|
||||
while($start -ge 0) {
|
||||
$a = siftDown $a $start ($count-1)
|
||||
$start--
|
||||
}
|
||||
$a
|
||||
}
|
||||
function siftdown($a, $start, $end) {
|
||||
$b, $root = $true, $start
|
||||
while(( ($root * 2 + 1) -le $end) -and $b) {
|
||||
$child = $root * 2 + 1
|
||||
if( ($child + 1 -le $end) -and ($a[$child] -lt $a[$child + 1]) ) {
|
||||
$child++
|
||||
}
|
||||
if($a[$root] -lt $a[$child]) {
|
||||
$a[$root], $a[$child] = $a[$child], $a[$root]
|
||||
$root = $child
|
||||
}
|
||||
else { $b = $false}
|
||||
}
|
||||
$a
|
||||
}
|
||||
$array = @(60, 21, 19, 36, 63, 8, 100, 80, 3, 87, 11)
|
||||
"$(heapsort $array $array.Count)"
|
||||
|
|
@ -1,42 +1,29 @@
|
|||
/*REXX program sorts an array using the heapsort algorithm. */
|
||||
call gen@ /*generate the array elements. */
|
||||
call show@ 'before sort' /*show the before array elements*/
|
||||
call heapSort # /*invoke the heap sort. */
|
||||
call show@ ' after sort' /*show the after array elements*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────HEAPSORT subroutine─────────────────*/
|
||||
/*REXX pgm sorts an array (modern Greek alphabet) using a heapsort algorithm. */
|
||||
@.=; @.1='alpha' ; @.6 ='zeta' ; @.11='lambda' ; @.16='pi' ; @.21='phi'
|
||||
@.2='beta' ; @.7 ='eta' ; @.12='mu' ; @.17='rho' ; @.22='chi'
|
||||
@.3='gamma' ; @.8 ='theta'; @.13='nu' ; @.18='sigma' ; @.23='psi'
|
||||
@.4='delta' ; @.9 ='iota' ; @.14='xi' ; @.19='tau' ; @.24='omega'
|
||||
@.5='epsilon'; @.10='kappa'; @.15='omicron'; @.20='upsilon'
|
||||
do #=1 while @.#\==''; end; #=#-1 /*find # entries*/
|
||||
call show "before sort:"
|
||||
call heapSort #; say copies('▒',40) /*sort; show sep*/
|
||||
call show " after sort:"
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
heapSort: procedure expose @.; parse arg n; do j=n%2 by -1 to 1
|
||||
call shuffle j,n
|
||||
end /*j*/
|
||||
do n=n by -1 to 2
|
||||
_=@.1; @.1=@.n; @.n=_; call shuffle 1,n-1 /*swap and shuffle.*/
|
||||
end /*n*/
|
||||
return
|
||||
/*──────────────────────────────────SHUFFLE subroutine──────────────────*/
|
||||
shuffle: procedure expose @.; parse arg i,n; _=@.i
|
||||
do while i+i<=n; j=i+i; k=j+1
|
||||
if k<=n then if @.k>@.j then j=k
|
||||
if _>=@.j then leave
|
||||
@.i=@.j; i=j
|
||||
end /*while*/
|
||||
@.i=_
|
||||
return
|
||||
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
|
||||
gen@: @.=; @.1='---modern Greek alphabet letters---' /*default; title.*/
|
||||
@.2= copies('=', length(@.1)) /*match sep with ↑*/
|
||||
@.3='alpha' ; @.9 ='eta' ; @.15='nu' ; @.21='tau'
|
||||
@.4='beta' ; @.10='theta' ; @.16='xi' ; @.22='upsilon'
|
||||
@.5='gamma' ; @.11='iota' ; @.17='omicron' ; @.23='phi'
|
||||
@.6='delta' ; @.12='kappa' ; @.18='pi' ; @.24='chi'
|
||||
@.7='epsilon' ; @.13='lambd' ; @.19='rho' ; @.25='psi'
|
||||
@.8='zeta' ; @.14='mu' ; @.20='sigma' ; @.26='omega'
|
||||
|
||||
do #=1 while @.#\==''; end /*find how many entries in list. */
|
||||
#=#-1 /*adjust highItem slightly. */
|
||||
return
|
||||
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
|
||||
show@: do j=1 for # /* [↓] display elements in array.*/
|
||||
say ' element' right(j,length(#)) arg(1)':' @.j
|
||||
end /*j*/
|
||||
say copies('■', 70) /*show a separator line. */
|
||||
do n=n by -1 to 2
|
||||
_=@.1; @.1=@.n; @.n=_; call shuffle 1,n-1 /*swap and shuffle.*/
|
||||
end /*n*/
|
||||
return
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
shuffle: procedure expose @.; parse arg i,n; $=@.i /*obtain the parent*/
|
||||
do while i+i<=n; j=i+i; k=j+1
|
||||
if k<=n then if @.k>@.j then j=k
|
||||
if $>=@.j then leave
|
||||
@.i=@.j; i=j
|
||||
end /*while*/
|
||||
@.i=$; return
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
show: do e=1 for #; say ' element' right(e,length(#)) arg(1) @.e; end; return
|
||||
|
|
|
|||
|
|
@ -1,72 +1,26 @@
|
|||
/* REXX ***************************************************************
|
||||
* Translated from PL/I
|
||||
* 27.07.2013 Walter Pachl
|
||||
**********************************************************************/
|
||||
list='---letters of the modern Greek Alphabet---|'||,
|
||||
'==========================================|'||,
|
||||
'alpha|beta|gamma|delta|epsilon|zeta|eta|theta|'||,
|
||||
'iota|kappa|lambda|mu|nu|xi|omicron|pi|'||,
|
||||
'rho|sigma|tau|upsilon|phi|chi|psi|omega'
|
||||
Do i=0 By 1 While list<>''
|
||||
Parse Var list a.i '|' list
|
||||
End
|
||||
n=i-1
|
||||
|
||||
Call showa 'before sort'
|
||||
Call heapsort n
|
||||
Call showa ' after sort'
|
||||
Exit
|
||||
|
||||
heapSort: Procedure Expose a.
|
||||
Parse Arg count
|
||||
Call heapify count
|
||||
end=count-1
|
||||
do while end>0
|
||||
Call swap end,0
|
||||
end=end-1
|
||||
Call siftDown 0,end
|
||||
End
|
||||
Return
|
||||
|
||||
heapify: Procedure Expose a.
|
||||
Parse Arg count
|
||||
start=(count-2)%2
|
||||
Do while start>=0
|
||||
Call siftDown start,count-1
|
||||
start=start-1
|
||||
End
|
||||
Return
|
||||
|
||||
siftDown: Procedure Expose a.
|
||||
Parse Arg start,end
|
||||
root=start
|
||||
Do while root*2+1<= end
|
||||
child=root*2+1
|
||||
sw=root
|
||||
if a.sw<a.child Then
|
||||
sw=child
|
||||
child_1=child+1
|
||||
if child+1<=end & a.sw<a.child_1 Then
|
||||
sw=child+1
|
||||
if sw<>root Then Do
|
||||
Call swap root,sw
|
||||
root=sw
|
||||
End
|
||||
else
|
||||
return
|
||||
End
|
||||
Return
|
||||
|
||||
swap: Procedure Expose a.
|
||||
Parse arg x,y
|
||||
temp=a.x
|
||||
a.x=a.y
|
||||
a.y=temp
|
||||
Return
|
||||
|
||||
showa: Procedure Expose a. n
|
||||
Parse Arg txt
|
||||
Do j=0 To n-1
|
||||
Say 'element' format(j,2) txt a.j
|
||||
End
|
||||
Return
|
||||
/*REXX pgm sorts an array (modern Greek alphabet) using a heapsort algorithm. */
|
||||
g = 'alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu nu xi',
|
||||
"omicron pi rho sigma tau upsilon phi chi psi omega" /*adjust # [↓] */
|
||||
do #=1 for words(g); @.#=word(g,#); end; #=#-1
|
||||
call show "before sort:"
|
||||
call heapSort #; say copies('▒',40) /*sort; show sep*/
|
||||
call show " after sort:"
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
heapSort: procedure expose @.; parse arg n; do j=n%2 by -1 to 1
|
||||
call shuffle j,n
|
||||
end /*j*/
|
||||
do n=n by -1 to 2
|
||||
_=@.1; @.1=@.n; @.n=_; call shuffle 1,n-1 /*swap and shuffle.*/
|
||||
end /*n*/
|
||||
return
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
shuffle: procedure expose @.; parse arg i,n; $=@.i /*obtain the parent*/
|
||||
do while i+i<=n; j=i+i; k=j+1
|
||||
if k<=n then if @.k>@.j then j=k
|
||||
if $>=@.j then leave
|
||||
@.i=@.j; i=j
|
||||
end /*while*/
|
||||
@.i=$; return
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
show: do e=1 for #; say ' element' right(e,length(#)) arg(1) @.e; end; return
|
||||
|
|
|
|||
|
|
@ -0,0 +1,72 @@
|
|||
/* REXX ***************************************************************
|
||||
* Translated from PL/I
|
||||
* 27.07.2013 Walter Pachl
|
||||
**********************************************************************/
|
||||
list='---letters of the modern Greek Alphabet---|'||,
|
||||
'==========================================|'||,
|
||||
'alpha|beta|gamma|delta|epsilon|zeta|eta|theta|'||,
|
||||
'iota|kappa|lambda|mu|nu|xi|omicron|pi|'||,
|
||||
'rho|sigma|tau|upsilon|phi|chi|psi|omega'
|
||||
Do i=0 By 1 While list<>''
|
||||
Parse Var list a.i '|' list
|
||||
End
|
||||
n=i-1
|
||||
|
||||
Call showa 'before sort'
|
||||
Call heapsort n
|
||||
Call showa ' after sort'
|
||||
Exit
|
||||
|
||||
heapSort: Procedure Expose a.
|
||||
Parse Arg count
|
||||
Call heapify count
|
||||
end=count-1
|
||||
do while end>0
|
||||
Call swap end,0
|
||||
end=end-1
|
||||
Call siftDown 0,end
|
||||
End
|
||||
Return
|
||||
|
||||
heapify: Procedure Expose a.
|
||||
Parse Arg count
|
||||
start=(count-2)%2
|
||||
Do while start>=0
|
||||
Call siftDown start,count-1
|
||||
start=start-1
|
||||
End
|
||||
Return
|
||||
|
||||
siftDown: Procedure Expose a.
|
||||
Parse Arg start,end
|
||||
root=start
|
||||
Do while root*2+1<= end
|
||||
child=root*2+1
|
||||
sw=root
|
||||
if a.sw<a.child Then
|
||||
sw=child
|
||||
child_1=child+1
|
||||
if child+1<=end & a.sw<a.child_1 Then
|
||||
sw=child+1
|
||||
if sw<>root Then Do
|
||||
Call swap root,sw
|
||||
root=sw
|
||||
End
|
||||
else
|
||||
return
|
||||
End
|
||||
Return
|
||||
|
||||
swap: Procedure Expose a.
|
||||
Parse arg x,y
|
||||
temp=a.x
|
||||
a.x=a.y
|
||||
a.y=temp
|
||||
Return
|
||||
|
||||
showa: Procedure Expose a. n
|
||||
Parse Arg txt
|
||||
Do j=0 To n-1
|
||||
Say 'element' format(j,2) txt a.j
|
||||
End
|
||||
Return
|
||||
Loading…
Add table
Add a link
Reference in a new issue