Sorting algorithms/Quicksort: Difference between revisions
Content added Content deleted
m (→ES5) |
m (→{{header|REXX}}: added/changed whitespace and comments.) |
||
Line 4,163: | Line 4,163: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===version 1=== |
===version 1=== |
||
<lang rexx>/*REXX program sorts a stemmed array using the quicksort algorithm.*/ |
<lang rexx>/*REXX program sorts a stemmed array using the quicksort algorithm. */ |
||
call gen@ /*generate the |
call gen@ /*generate the elements for the array. */ |
||
call show@ 'before sort' /*show before array elements.*/ |
call show@ 'before sort' /*show the before array elements. */ |
||
call quickSort # /*invoke the quicksort |
call quickSort # /*invoke the quicksort subroutine. */ |
||
call show@ ' after sort' /*show after array elements.*/ |
call show@ ' after sort' /*show the after array elements. */ |
||
exit /*stick a fork in it, we're done.*/ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
/*──────────────────────────────────QUICKSORT subroutine────────────────*/ |
|||
quickSort: procedure expose @. /*access the caller's local |
quickSort: procedure expose @. /*access the caller's local variable. */ |
||
a.1=1; b.1=arg(1); $=1 |
a.1=1; b.1=arg(1); $=1 |
||
do while $\==0; L=a.$; t=b.$; $=$-1; |
do while $\==0; L=a.$; t=b.$; $=$-1; if t<2 then iterate |
||
h=L+t-1 |
h=L+t-1; ?=L+t%2 |
||
if @.h<@.L then if @.?<@.h then do; p=@.h; @.h=@.L; end |
|||
?=L+t%2 |
|||
else if @.?>@.L then p=@.L |
|||
else |
else do; p=@.?; @.?=@.L; end |
||
else if @.?<@.l then p=@.L |
|||
else if @.? |
else if @.?>@.h then do; p=@.h; @.h=@.L; end |
||
else do; p=@.?; @.?=@.L; end |
|||
j=L+1; k=h |
|||
⚫ | |||
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 " " */ |
|||
⚫ | |||
if j>=k then leave /*segment finished? */ |
|||
_=@.j; @.j=@.k; @.k=_ /*swap J&K elements.*/ |
|||
end /*forever*/ |
|||
$=$+1 |
|||
⚫ | |||
k=j-1; @.L=@.k; @.k=p |
|||
⚫ | |||
eLse do; a.$=L; b.$=k-L; $=$+1; a.$=j; b.$=h-j+1; end |
|||
⚫ | |||
return |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
⚫ | |||
show@: widthH=length(#) /*maximum width of any line of output. */ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
/*──────────────────────────────────GEN@ subroutine─────────────────────*/ |
|||
⚫ | |||
gen@: @.=; maxL=0 /*assign default value for array.*/ |
|||
/*──────────────────────────────────GEN@ subroutine──────────────────────────────────────────────────────────────────────────────────────────────────────*/ |
|||
⚫ | |||
@ |
gen@: @.=; maxL=0 /*assign default value for array.*/ |
||
⚫ | |||
@.2 = '=' /*this value is expanded later. */ |
|||
@.3 = "Perdido River Alabama, Florida" |
@.3 = "Perdido River Alabama, Florida" |
||
@.4 = "Chattahoochee River Alabama, Georgia" |
@.4 = "Chattahoochee River Alabama, Georgia" |
||
Line 4,263: | Line 4,268: | ||
@.63 = "Columbia River Oregon, Washington" |
@.63 = "Columbia River Oregon, Washington" |
||
do #=1 while @.#\=='' /*find how many entries, and |
do #=1 while @.#\=='' /*find how many entries in array, and */ |
||
maxL=max(maxL, length(@.#)) /* find the maximum width entry.*/ |
maxL=max(maxL, length(@.#)) /* also find the maximum width entry.*/ |
||
end /*#*/ |
end /*#*/ |
||
#=#-1 /*adjust the highest element |
#=#-1 /*adjust the highest element number. */ |
||
@.1= |
@.1=center(@.1, maxL, '-') /* " " header information. */ |
||
@.2=copies(@.2, maxL) /* |
@.2=copies(@.2, maxL) /* " " " separator. */ |
||
return |
|||
/*──────────────────────────────────SHOW@ subroutine────────────────────*/ |
|||
show@: widthH=length(#) /*maximum width of any line. */ |
|||
⚫ | |||
⚫ | |||
end /*j*/ |
|||
⚫ | |||
return</lang> |
return</lang> |
||
'''output''' |
|||
{{out}} |
|||
<pre style="height: |
<pre style="height:60ex"> |
||
element 1 before sort: ------------------------------------------------ Rivers that form part of a (USA) state's border ------------------------------------------------- |
element 1 before sort: ------------------------------------------------ Rivers that form part of a (USA) state's border ------------------------------------------------- |
||
element 2 before sort: ================================================================================================================================================== |
element 2 before sort: ================================================================================================================================================== |