Anonymous user
Sorting algorithms/Heapsort: Difference between revisions
m
→{{header|REXX}}: restructured 1st two programs, add/changed comments and whitespace.
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:
<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*/
@.=;
do #=1 while @.#\==''; end; #=#-1
call show "before sort:"
call heapSort
call show " after sort:"
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
heapSort:
do n=n by -1 to 2; _=@.1; @.1=@.n;
end /*n*/ /* [↑] swap two elements; and
/*──────────────────────────────────────────────────────────────────────────────────────*/
shuffle:
end
@.i=$;
/*──────────────────────────────────────────────────────────────────────────────────────*/
show:
''output''' 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
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"
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:
do n=n by -1 to 2; _=@.1; @.1=@.n;
end /*n*/ /* [↑] swap two elements; and
/*──────────────────────────────────────────────────────────────────────────────────────*/
shuffle:
end
@.i=$;
/*──────────────────────────────────────────────────────────────────────────────────────*/
show:
'''output''' is the same as the 1<sup>st</sup> REXX version.
|