Sorting algorithms/Heapsort: Difference between revisions

m
→‎version 1: compressed assignments for array, changed DO loop indentatio, added/changed comments. -- ~~~~
m (→‎version 1: removed extra blanks lines from program, changed array header information (title) and added generic method for title header, added whitespace. -- ~~~~)
m (→‎version 1: compressed assignments for array, changed DO loop indentatio, added/changed comments. -- ~~~~)
Line 2,151:
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────HEAPSORT subroutine─────────────────*/
heapSort: procedure expose @.; parse arg n; do j=n%2 by -1 to 1
do j=n%2 by -1call shuffle to 1j,n
call shuffle end /*j,n*/
do n=n endby -1 /*n*/to 2
end /*j*/
_=@.1; @.1=@.n; do @.n=n_; bycall shuffle 1,n-1 to/*swap & 2reshuffle.*/
end /*jn*/
_=@.1; @.1=@.n; @.n=_; call shuffle 1,n-1
end /*n*/
return
/*──────────────────────────────────SHUFFLE subroutine──────────────────*/
shuffle: procedure expose @.; parse arg i,n; _=@.i
do while i+i<=n; j=i+i; k=j+1
j=i+i; if k<=n & @.k>@.j+1 then j=k
if k<=n & @.k if _>=@.j then j=kleave
if _>=@.j then leave @.i=@.j; i=j
@.i=@.j; end /*while i=j+i≤n*/
end /*while i+i<=n*/
@.i=_
return
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
gen@: @.= /*assign default value for array.*/
@.1 = '---modern Greek alphabet letters---' ; @.14 = 'mu'
@.2 = copies('=', length(@.1)) ; @.15 = 'nu'
@.3 = 'alpha' ; @.11 = 'iota' ; @.1619 = 'xirho'
@.4 = 'beta' ; @.12 = 'kappa' ; @.1720 = 'omicronsigma'
@.5 = 'gamma' ; @.13 = 'lambda' ; @.1821 = 'pitau'
@.6 = 'delta' ; @.14 = 'mu' ; @.1922 = 'rhoupsilon'
@.7 = 'epsilon' ; @.15 = 'nu' ; @.2023 = 'sigmaphi'
@.8 = 'zeta' ; @.16 = 'xi' ; @.2124 = 'tauchi'
@.9 = 'eta' ; @.17 = 'omicron' ; @.2225 = 'upsilonpsi'
@.10 = 'theta' ; @.18 = 'pi' ; @.2326 = 'phiomega'
@.11 = 'iota' ; @.2419 = 'chirho'
do highItem=1 while @.highItem\==''; end /*find how many entries. */
@.12 = 'kappa' ; @.25 = 'psi'
@.13 highItem=highItem-1 'lambda' /*adjust highItem ; @slightly.26 = 'omega'*/
do highItem=1 while @.highItem\=='' /*find how many entries. */
end /*highitem*/
highItem=highItem-1 /*adjust highItem slightly. */
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@: widthH=length(highItem) /*maximum width of any line. do j=1 for */highItem
say 'element' right(j,length(highItem)) arg(1)':' do @.j=1 for highItem
end say 'element' right(j,widthH) arg(1)':' @./*j*/
end /*j*/
say copies('─', 79) /*show a separator line. */
return</lang>
'''output''' &nbsp; using the Greek alpahetalphabet for input
<pre style="height:30ex;overflow:scroll">
element 1 before sort: ---modern Greek alphabet letters---