Sorting algorithms/Heapsort: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Perl 6}}: removed 'is rw') |
m (→{{header|REXX}}: restructured 1st two programs, add/changed comments and whitespace.) |
||
Line 2,839: | Line 2,839: | ||
<br>Indexing of the array (for this version) starts with '''1''' (one), but can be programmed to start with zero. |
<br>Indexing of the array (for this version) starts with '''1''' (one), but can be programmed to start with zero. |
||
<lang rexx>/*REXX program sorts an array (names of modern Greek letters) using a heapsort algorithm*/ |
<lang rexx>/*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 |
do #=1 while @.#\==''; end; #=#-1 /*find # entries.*/ |
||
call show "before sort:" |
call show "before sort:" |
||
call heapSort |
call heapSort #; say copies('▒', 40) /*sort; show sep.*/ |
||
call show " after sort:" |
call show " after sort:" |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
heapSort: |
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 |
|||
end /*n*/ /* [↑] swap two elements; and shuffle.*/ |
|||
return |
|||
_=@.1; @.1=@.n; @.n=_; call shuffle 1, n-1 /*swap and shuffle.*/ |
|||
end /*n*/ |
|||
return |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
shuffle: |
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 |
|||
@.i=$; return |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
show: |
show: do e=1 for #; say ' element' right(e,length(#)) arg(1) @.e; end; return</lang> |
||
''output''' using the (default) Greek alphabet for input: |
''output''' using the (default) Greek alphabet for input: |
||
<pre> |
<pre> |
||
Line 2,922: | Line 2,918: | ||
===version 2=== |
===version 2=== |
||
This REXX version creates a stemmed array from a list. |
This REXX version creates a stemmed array from a list. |
||
<lang rexx>/*REXX program sorts |
<lang rexx>/*REXX program sorts a list (names of modern Greek letters) using a heapsort algorithm.*/ |
||
g= 'alpha beta gamma delta epsilon zeta eta theta iota kappa lambda |
g= 'alpha beta gamma delta epsilon zeta eta theta iota kappa lambda', |
||
"omicron pi rho sigma tau upsilon phi chi psi omega" |
"mu nu xi omicron pi rho sigma tau upsilon phi chi psi omega" /*adjust # [↓] */ |
||
do #=1 for words(g); @.#=word(g,#); end; #=#-1 |
do #=1 for words(g); @.#=word(g,#); end; #=#-1 |
||
call show "before sort:" |
call show "before sort:" |
||
Line 2,931: | Line 2,927: | ||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
heapSort: |
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 |
|||
end /*n*/ /* [↑] swap two elements; and shuffle.*/ |
|||
return |
|||
_=@.1; @.1=@.n; @.n=_; call shuffle 1, n-1 /*swap and shuffle.*/ |
|||
end /*n*/ |
|||
return |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
shuffle: |
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 |
|||
@.i=$; return |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
show: |
show: do e=1 for #; say ' element' right(e,length(#)) arg(1) @.e; end; return</lang> |
||
'''output''' is the same as the 1<sup>st</sup> REXX version. |
'''output''' is the same as the 1<sup>st</sup> REXX version. |
||