Sorting algorithms/Quicksort: Difference between revisions

m
→‎{{header|REXX}}: added/changed whitespace and comments.
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 array elements. for the array. */
call show@ 'before sort' /*show the before array elements. */
call quickSort # /*invoke the quicksort routinesubroutine. */
call show@ ' after sort' /*show the after array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────QUICKSORT subroutine────────────────*/
quickSort: procedure expose @. /*access the caller's local varvariable. */
a.1=1; b.1=arg(1); $=1
 
do while $\==0; L=a.$; t=b.$; $=$-1; if t<2 then iterate
h=L+t-1; ?=L+t%2
if @.h<@.L then if @.?<@.h then do; p=@.h; @.h=@.L; end
?=L+t%2
if @.h<@.L then else if @.?<>@.hL then do; p=@.h; @.h=@.L; end
else ifdo; p=@.?>; @.L then p?=@.L; end
else do;if p=@.?; <@.?l then p=@.L; end
else if @.?<>@.lh then do; p=@.h; @.h=@.L; end
else if @.?>@.h then else do; p=@.h?; @.h?=@.L; end
j=L+1; else do; pk=@.?; @.?=@.L; endh
do forever
j=L+1
do j=j while j<=k & @.j<=p; end /*a tinie-tiny loop.*/
k=h
do k=k by -1 while j <k & @.k>=p; end /*another " " */
do forever
do if j>=jk then leave while j<=k & @.j<=p; end /*asegment tinie-tinyfinished? loop*/
do k=k by -1 while _=@.j; < @.j=@.k; & @.k>=p;_ end /*another " " /*swap J&K elements.*/
if j>=k then leave end /*segment finished?forever*/
$=$+1
_=@.j; @.j=@.k; @.k=_ /*swap j&k elements*/
end /*forever*/ k=j-1; @.L=@.k; @.k=p
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*/
 
k=j-1; @.L=@.k; @.k=p; $=$+1return
/*──────────────────────────────────────────────────────────────────────────────────────*/
if j<=? then do; a.$=j; b.$=h-j+1; $=$+1; a.$=L; b.$=k-L; end
show@: widthH=length(#) eLse do; a.$=L; b.$=k-L; $=$+1; a.$=j; b.$=h-j+1; /*maximum width of any line of output. end*/
do j=1 for # /*display each item in the array. */
end /*whiLe $¬==0*/
say 'element' right(j,widthH) arg(1)':' @.j
 
_=@.j; @.j=@.k; @.k=_ end /*swap j&k elements*/
return
say copies('▒', maxL + widthH + 22) /*display a separator (between line. outputs)*/
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
return
gen@: @.=; maxL=0 /*assign default value for array.*/
/*──────────────────────────────────GEN@ subroutine──────────────────────────────────────────────────────────────────────────────────────────────────────*/
@.1 = " Rivers that form part of a (USA) state's border " /*this value is adjusted later to include a prefix & suffix.*/
gen@.2: @.= '=' ; maxL=0 /*thisassign default value isfor expanded laterarray. */
@.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 also*/
maxL=max(maxL, length(@.#)) /* also find the maximum width entry.*/
end /*#*/
#=#-1 /*adjust the highest element #number. */
@.1=centrecenter(@.1, maxL, '-') /*adjust the " " header information. */
@.2=copies(@.2, maxL) /*adjust the header " " " separator. */
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@: widthH=length(#) /*maximum width of any line. */
do j=1 for # /*display each item in the array.*/
say 'element' right(j,widthH) arg(1)':' @.j
end /*j*/
say copies('▒', maxL + widthH + 22) /*display a separator line. */
return</lang>
'''output'''
{{out}}
<pre style="height:40ex60ex">
element 1 before sort: ------------------------------------------------ Rivers that form part of a (USA) state's border -------------------------------------------------
element 2 before sort: ==================================================================================================================================================