Sorting algorithms/Heapsort: Difference between revisions

m
→‎{{header|REXX}}: restructured 1st two programs, add/changed comments and whitespace.
m (→‎{{header|Perl 6}}: removed 'is rw')
m (→‎{{header|REXX}}: restructured 1st two programs, add/changed comments and whitespace.)
Line 2,839:
<br>Indexing of the array (for this version) starts with &nbsp; '''1''' &nbsp; (one), &nbsp; but can be programmed to start with zero.
<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 /*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 j1,n-1
end /*n*/ /* [↑] swap two elements; and end /*jshuffle.*/
do n=n by -1 to 2return
_=@.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 @.i=@.j; i=j/*while*/
@.i=$; end /*while*/return
@.i=$; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do e=1 for #; say ' element' right(e,length(#)) arg(1) @.e; end; return</lang>
''output''' &nbsp; using the (default) Greek alphabet for input:
<pre>
Line 2,922 ⟶ 2,918:
===version 2===
This REXX version creates a stemmed array from a list.
<lang rexx>/*REXX program sorts ana arraylist (names of modern Greek letters) using a heapsort algorithm.*/
g= 'alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu nu xi',
"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:"
Line 2,931 ⟶ 2,927:
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 j1,n-1
end /*n*/ /* [↑] swap two elements; and end /*jshuffle.*/
do n=n by -1 to 2return
_=@.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 @.i=@.j; i=j/*while*/
@.i=$; end /*while*/return
@.i=$; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do e=1 for #; say ' element' right(e,length(#)) arg(1) @.e; end; return</lang>
'''output''' &nbsp; is the same as the 1<sup>st</sup> REXX version.