Anonymous user
Sorting algorithms/Quicksort: Difference between revisions
m
→{{header|REXX}}: added/changed whitespace and comments.
m (→ES5) |
m (→{{header|REXX}}: added/changed whitespace and comments.) |
||
Line 4,163:
=={{header|REXX}}==
===version 1===
<lang rexx>/*REXX program sorts a stemmed array using the quicksort algorithm. */
call gen@ /*generate the
call show@ 'before sort' /*show the before array elements. */
call quickSort # /*invoke the quicksort
call show@ ' after sort' /*show the after array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
quickSort: procedure expose @. /*access the caller's local
a.1=1; b.1=arg(1); $=1
do while $\==0; L=a.$; t=b.$; $=$-1;
h=L+t-1; ?=L+t%2
if @.h<@.L then if @.?<@.h then do; p=@.h; @.h=@.L; end
else
else if @.?
j=L+1;
do forever▼
do j=j while j<=k & @.j<=p; end /*a tinie-tiny loop.*/
do k=k by -1 while j <k & @.k>=p; end /*another " " */
▲ do forever
$=$+1
_=@.j; @.j=@.k; @.k=_ /*swap j&k elements*/▼
if j<=? then do; a.$=j; b.$=h-j+1; $=$+1; a.$=L; b.$=k-L; end▼
eLse do; a.$=L; b.$=k-L; $=$+1; a.$=j; b.$=h-j+1; end
end /*whiLe $¬==0*/▼
/*──────────────────────────────────────────────────────────────────────────────────────*/
▲ if j<=? then do; a.$=j; b.$=h-j+1; $=$+1; a.$=L; b.$=k-L; end
show@: widthH=length(#)
do j=1 for # /*display each item in the array. */▼
▲ end /*whiLe $¬==0*/
say 'element' right(j,widthH) arg(1)':' @.j▼
return▼
▲ return
/*──────────────────────────────────GEN@ subroutine──────────────────────────────────────────────────────────────────────────────────────────────────────*/
@.1 = " Rivers that form part of a (USA) state's border " /*this value is adjusted later to include a prefix & suffix.*/▼
gen@
▲@.1 = " Rivers that form part of a (USA) state's border " /*this value is adjusted later to include a prefix & suffix.*/
@.2 = '=' /*this value is expanded later. */
@.3 = "Perdido River Alabama, Florida"
@.4 = "Chattahoochee River Alabama, Georgia"
Line 4,263 ⟶ 4,268:
@.63 = "Columbia River Oregon, Washington"
do #=1 while @.#\=='' /*find how many entries in array, and
maxL=max(maxL, length(@.#)) /* also find the maximum width entry.*/
end /*#*/
#=#-1 /*adjust the highest element
@.1=
@.2=copies(@.2, maxL) /*
▲ do j=1 for # /*display each item in the array.*/
▲ say 'element' right(j,widthH) arg(1)':' @.j
▲say copies('▒', maxL + widthH + 22) /*display a separator line. */
return</lang>
'''output'''
<pre style="height:
element 1 before sort: ------------------------------------------------ Rivers that form part of a (USA) state's border -------------------------------------------------
element 2 before sort: ==================================================================================================================================================
|