Anonymous user
Sorting algorithms/Heapsort: Difference between revisions
m
→version 1: removed extra blanks lines from program, changed array header information (title) and added generic method for title header, added whitespace. -- ~~~~
m (→{{header|REXX}}: added "-1" to a CALL. -- ~~~~) |
m (→version 1: removed extra blanks lines from program, changed array header information (title) and added generic method for title header, added whitespace. -- ~~~~) |
||
Line 2,146:
<lang rexx>/*REXX program sorts an array using the heapsort method. */
call gen@ /*generate the array elements. */
call show@ 'before sort'
call heapSort
call show@ ' after sort'
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────HEAPSORT subroutine─────────────────*/
heapSort: procedure expose @.; parse arg n
▲ end /*j*/
do n=n by -1 to 2
_=@.1; @.1=@.n; @.n=_; call shuffle 1,n-1
Line 2,162 ⟶ 2,161:
/*──────────────────────────────────SHUFFLE subroutine──────────────────*/
shuffle: procedure expose @.; parse arg i,n; _=@.i
do while i+i<=n
j=i+i; k=j+1
Line 2,173 ⟶ 2,171:
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
gen@: @.= /*assign default value for array.*/
@.1 = '---
@.2 = copies('
@.3 = 'alpha'
@.4 = 'beta'
@.5 = 'gamma'
@.6 = 'delta'
@.7 = 'epsilon'
@.8 = 'zeta'
@.9 = 'eta'
@.10 = 'theta'
@.11 = 'iota'
@.12 = 'kappa'
@.13 = 'lambda'
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,widthH) arg(1)':' @.j
Line 2,200 ⟶ 2,195:
say copies('─', 79) /*show a separator line. */
return</lang>
'''output''' using the Greek alpahet for input
<pre style="height:30ex;overflow:scroll">
element 1 before sort: ---
element 2 before sort:
element 3 before sort: alpha
element 4 before sort: beta
Line 2,229 ⟶ 2,224:
element 26 before sort: omega
───────────────────────────────────────────────────────────────────────────────
element 1 after sort: ---
element 2 after sort:
element 3 after sort: alpha
element 4 after sort: beta
Line 2,257 ⟶ 2,252:
───────────────────────────────────────────────────────────────────────────────
</pre>
=== Version 2 ===
<lang rexx>/* REXX ***************************************************************
|