June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,36 @@
|
|||
swap := proc(arr, a, b)
|
||||
local temp:
|
||||
temp := arr[a]:
|
||||
arr[a] := arr[b]:
|
||||
arr[b] := temp:
|
||||
end proc:
|
||||
heapify := proc(toSort, n, i)
|
||||
local largest, l, r, holder:
|
||||
largest := i:
|
||||
l := 2*i:
|
||||
r := 2*i+1:
|
||||
if (l <= n and toSort[l] > toSort[largest]) then
|
||||
largest := l:
|
||||
end if:
|
||||
if (r <= n and toSort[r] > toSort[largest]) then
|
||||
largest := r:
|
||||
end if:
|
||||
if (not largest = i) then
|
||||
swap(toSort, i, largest);
|
||||
heapify(toSort, n, largest):
|
||||
end if:
|
||||
end proc:
|
||||
heapsort := proc(arr)
|
||||
local n,i:
|
||||
n := numelems(arr):
|
||||
for i from trunc(n/2) to 1 by -1 do
|
||||
heapify(arr, n, i):
|
||||
end do:
|
||||
for i from n to 2 by -1 do
|
||||
swap(arr, 1, i):
|
||||
heapify(arr, i-1, 1):
|
||||
end do:
|
||||
end proc:
|
||||
arr := Array([17,3,72,0,36,2,3,8,40,0]);
|
||||
heapsort(arr);
|
||||
arr;
|
||||
|
|
@ -1,17 +1,18 @@
|
|||
/*REXX program sorts an array (names of modern Greek letters) 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.*/
|
||||
/*REXX pgm sorts an array (names of epichoric Greek letters) using a heapsort algorithm.*/
|
||||
@.=; @.1= 'alpha' ; @.7 = "zeta" ; @.13= 'mu' ; @.19= "qoppa" ; @.25= 'chi'
|
||||
@.2= 'beta' ; @.8 = "eta" ; @.14= 'nu' ; @.20= "rho" ; @.26= 'psi'
|
||||
@.3= 'gamma' ; @.9 = "theta" ; @.15= 'xi' ; @.21= "sigma" ; @.27= 'omega'
|
||||
@.4= 'delta' ; @.10= "iota" ; @.16= 'omicron'; @.22= "tau"
|
||||
@.5= 'digamma'; @.11= "kappa" ; @.17= 'pi' ; @.23= "upsilon"
|
||||
@.6= 'epsilon'; @.12= "lambda"; @.18= 'san' ; @.24= "phi"
|
||||
do #=1 until @.#==''; 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 @.; 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
|
||||
do n=n by -1 to 2; _=@.1; @.1=@.n; @.n=_; call shuffle 1, n-1
|
||||
end /*n*/ /* [↑] swap two elements; and shuffle.*/
|
||||
return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
|
|
@ -22,4 +23,4 @@ shuffle: procedure expose @.; parse arg i,n; $=@.i /*obtain
|
|||
end /*while*/
|
||||
@.i=$; return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
show: do e=1 for #; say ' element' right(e,length(#)) arg(1) @.e; end; return
|
||||
show: do s=1 for #; say ' element' right(s, length(#)) arg(1) @.s; end; return
|
||||
|
|
|
|||
|
|
@ -1,23 +1,23 @@
|
|||
/*REXX program sorts a list (names of modern Greek letters) using a heapsort algorithm.*/
|
||||
parse arg g /*obtain optional argument from the CL.*/
|
||||
if g='' then 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
|
||||
/*REXX pgm sorts an array (names of epichoric Greek letters) using a heapsort algorithm.*/
|
||||
parse arg g /*obtain optional arguments from the CL*/
|
||||
if g='' then g='alpha beta gamma delta digamma epsilon zeta eta theta iota kappa lambda',
|
||||
"mu nu xi omicron pi san qoppa rho sigma tau upsilon phi chi psi omega"
|
||||
#=words(g); do i=1 for #; @.i=word(g,i); end /*assign to @. */
|
||||
call show "before sort:"
|
||||
call heapSort #; say copies('▒', 40) /*sort; show sep*/
|
||||
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 @.; 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
|
||||
do n=n by -1 to 2; _=@.1; @.1=@.n; @.n=_; call shuffle 1, n-1
|
||||
end /*n*/ /* [↑] swap two elements; and shuffle.*/
|
||||
return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
shuffle: procedure expose @.; parse arg i,n; $=@.i /*obtain parent. */
|
||||
shuffle: procedure expose @.; parse arg i,n; $=@.i /*obtain 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
|
||||
show: do s=1 for #; say ' element' right(s, length(#)) arg(1) @.s; end; return
|
||||
|
|
|
|||
|
|
@ -0,0 +1,53 @@
|
|||
# Project : Sorting algorithms/Heapsort
|
||||
# Date : 2018/03/04
|
||||
# Author : Gal Zsolt [~ CalmoSoft ~]
|
||||
# Email : <calmosoft@gmail.com>
|
||||
|
||||
test = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1]
|
||||
see "before sort:" + nl
|
||||
showarray(test)
|
||||
heapsort(test)
|
||||
see "after sort:" + nl
|
||||
showarray(test)
|
||||
|
||||
func heapsort(a)
|
||||
cheapify(a)
|
||||
for e = len(a) to 1 step -1
|
||||
temp = a[e]
|
||||
a[e] = a[1]
|
||||
a[1] = temp
|
||||
siftdown(a, 1, e-1)
|
||||
next
|
||||
|
||||
func cheapify(a)
|
||||
m = len(a)
|
||||
for s = floor((m - 1) / 2) to 1 step -1
|
||||
siftdown(a,s,m)
|
||||
next
|
||||
|
||||
func siftdown(a,s,e)
|
||||
r = s
|
||||
while r * 2 + 1 <= e
|
||||
c = r * 2
|
||||
if c + 1 <= e
|
||||
if a[c] < a[c + 1]
|
||||
c = c + 1
|
||||
ok
|
||||
ok
|
||||
if a[r] < a[c]
|
||||
temp = a[r]
|
||||
a[r] = a[c]
|
||||
a[c] = temp
|
||||
r = c
|
||||
else
|
||||
exit
|
||||
ok
|
||||
end
|
||||
|
||||
func showarray(vect)
|
||||
svect = ""
|
||||
for n = 1 to len(vect)
|
||||
svect = svect + vect[n] + " "
|
||||
next
|
||||
svect = left(svect, len(svect) - 1)
|
||||
see svect + nl
|
||||
|
|
@ -1,36 +1,40 @@
|
|||
fn main() {
|
||||
let mut v = [4,6,8,1,0,3,2,2,9,5];
|
||||
heap_sort(&mut v, |x,y| x < y);
|
||||
let mut v = [4, 6, 8, 1, 0, 3, 2, 2, 9, 5];
|
||||
heap_sort(&mut v, |x, y| x < y);
|
||||
println!("{:?}", v);
|
||||
}
|
||||
|
||||
fn heap_sort<T,F>(array: &mut [T], order: F)
|
||||
where F: Fn(&T,&T) -> bool
|
||||
fn heap_sort<T, F>(array: &mut [T], order: F)
|
||||
where
|
||||
F: Fn(&T, &T) -> bool,
|
||||
{
|
||||
let len = array.len();
|
||||
// Create heap
|
||||
for start in (0..len/2).rev() {
|
||||
sift_down(array,&order,start,len-1)
|
||||
for start in (0..len / 2).rev() {
|
||||
shift_down(array, &order, start, len - 1)
|
||||
}
|
||||
|
||||
for end in (1..len).rev() {
|
||||
array.swap(0,end);
|
||||
sift_down(array,&order,0,end-1)
|
||||
array.swap(0, end);
|
||||
shift_down(array, &order, 0, end - 1)
|
||||
}
|
||||
}
|
||||
|
||||
fn sift_down<T,F>(array: &mut [T], order: &F, start: usize, end: usize)
|
||||
where F: Fn(&T,&T) -> bool
|
||||
fn shift_down<T, F>(array: &mut [T], order: &F, start: usize, end: usize)
|
||||
where
|
||||
F: Fn(&T, &T) -> bool,
|
||||
{
|
||||
let mut root = start;
|
||||
loop {
|
||||
let mut child = root * 2 + 1;
|
||||
if child > end { break; }
|
||||
if child > end {
|
||||
break;
|
||||
}
|
||||
if child + 1 <= end && order(&array[child], &array[child + 1]) {
|
||||
child += 1;
|
||||
}
|
||||
if order(&array[root], &array[child]) {
|
||||
array.swap(root,child);
|
||||
array.swap(root, child);
|
||||
root = child
|
||||
} else {
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use std::collections::BinaryHeap;
|
||||
|
||||
fn main() {
|
||||
let src = vec![6,2,3,6,1,2,7,8,3,2];
|
||||
let sorted= BinaryHeap::from(src).into_sorted_vec();
|
||||
let src = vec![6, 2, 3, 6, 1, 2, 7, 8, 3, 2];
|
||||
let sorted = BinaryHeap::from(src).into_sorted_vec();
|
||||
println!("{:?}", sorted);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue