Sorting algorithms/Heapsort: Difference between revisions
Content added Content deleted
m (→{{header|AppleScript}}: Minor comment correction.) |
(→{{header|REXX}}: rewrote version 1, elided version 2, renumbered version 3 to version 2.) |
||
Line 3,795: | Line 3,795: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===version 1, elements of an array=== |
===version 1, elements of an array=== |
||
This REXX version uses a heapsort to sort elements of an array |
This REXX version uses a heapsort to sort elements of an array which is constructed from a list of words or numbers, |
||
<br>or a mixture of both. |
|||
Indexing of the array |
Indexing of the array starts with '''1''' (one), but can be programmed to start with zero. |
||
<lang rexx>/*REXX pgm sorts an array (names of epichoric Greek letters) using a heapsort algorithm.*/ |
<lang rexx>/*REXX pgm sorts an array (names of epichoric Greek letters) using a heapsort algorithm.*/ |
||
parse arg x; call init /*use args or default, define @ array.*/ |
|||
call show "before sort:" /*#: the number of elements in array*/ |
|||
call heapSort #; say copies('▒', 40) /*sort; then after sort, show separator*/ |
|||
⚫ | |||
@.4= 'delta' ; @.10= "iota" ; @.16= 'omicron'; @.22= "tau" |
|||
@.5= 'digamma'; @.11= "kappa" ; @.17= 'pi' ; @.23= "upsilon" |
|||
@.6= 'epsilon'; @.12= "lambda"; @.18= 'san' ; @.24= "phi" |
|||
⚫ | |||
call show "before sort:" |
|||
call heapSort #; say copies('▒', 40) /*sort; show sep.*/ |
|||
⚫ | |||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
⚫ | |||
heapSort: procedure expose @.; arg n; do j=n%2 by -1 to 1; call shuffle j,n; end /*j*/ |
|||
"xi omicron pi san qoppa rho sigma tau upsilon phi chi psi omega" |
|||
if x='' then x= _; #= words(x) /*#: number of words in X*/ |
|||
⚫ | |||
return |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
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*/; return /* [↑] swap two elements; and shuffle.*/ |
|||
⚫ | |||
⚫ | |||
shuffle: procedure expose @.; parse arg i,n; $= @.i /*obtain parent.*/ |
|||
⚫ | |||
@.i=$; return |
|||
⚫ | |||
⚫ | |||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
show: do s=1 for #; say ' element' right(s, length(#)) arg(1) @.s; end; return</lang> |
show: do s=1 for #; say ' element' right(s, length(#)) arg(1) @.s; end; return</lang> |
||
{{out|output|text= when using the default (epichoric Greek alphabet) for input:}} |
{{out|output|text= when using the default (epichoric Greek alphabet) for input:}} |
||
(Shown at three-quarter size.) |
|||
<pre> |
|||
<pre style="font-size:75%> |
|||
element 1 before sort: alpha |
element 1 before sort: alpha |
||
element 2 before sort: beta |
element 2 before sort: beta |
||
Line 3,882: | Line 3,881: | ||
element 27 after sort: zeta |
element 27 after sort: zeta |
||
</pre> |
</pre> |
||
⚫ | |||
(Shown at three-quarter size.) |
|||
<pre style="font-size:75%"> |
|||
===version 2, elements of a list=== |
|||
This REXX version creates a stemmed array from a list (it can be numbers or characters, or a mixture of both). |
|||
<lang rexx>/*REXX pgm sorts an array (names of epichoric Greek letters) using a heapsort algorithm.*/ |
|||
parse arg g /*obtain optional arguments from the CL*/ |
|||
⚫ | |||
"mu nu xi omicron pi san qoppa rho sigma tau upsilon phi chi psi omega" |
|||
⚫ | |||
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 @.; 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 |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
shuffle: procedure expose @.; parse arg i,n; $=@.i /*obtain parent.*/ |
|||
do while i+i<=n; j=i+i; k=j+1 |
|||
⚫ | |||
if $>=@.j then leave; @.i=@.j; i=j |
|||
end /*while*/ |
|||
@.i=$; return |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
show: do s=1 for #; say ' element' right(s, length(#)) arg(1) @.s; end; return</lang> |
|||
{{out|output|text= is identical to the 1<sup>st</sup> REXX version when using the default input (an epichoric Greek alphabet).}} |
|||
⚫ | |||
<pre> |
|||
element 1 before sort: 19 |
element 1 before sort: 19 |
||
element 2 before sort: 0 |
element 2 before sort: 0 |
||
Line 3,938: | Line 3,910: | ||
</pre> |
</pre> |
||
===version |
===version 2=== |
||
<lang rexx>/* REXX *************************************************************** |
<lang rexx>/* REXX *************************************************************** |
||
* Translated from PL/I |
* Translated from PL/I |