Sorting Algorithms/Circle Sort: Difference between revisions

Content deleted Content added
m →‎{{header|REXX}}: elided a blank line in output.
→‎{{header|REXX}}: simplified the REXX program by using a SWAP subroutine.
Line 302: Line 302:
=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program uses a circle sort to sort an array (or list) of numbers.*/
<lang rexx>/*REXX program uses a circle sort to sort an array (or list) of numbers.*/
parse arg x; if x='' then x=6 7 8 9 2 5 3 4 1
parse arg x; if x='' then x=6 7 8 9 2 5 3 4 1 /*use the defaults ? */
say 'before sort: ' x /* [↑] get numbers; use default?*/
say 'before sort: ' x /*display the unsorted list of #s*/
#=words(x) /*obtain the number of elements. */
#=words(x) /*obtain the number of elements. */
/*use an array instead of a list.*/

do i=1 for #; @.i=word(x,i); end /*assign numbers to the array @.*/
do i=1 for #; @.i=word(x,i); end /*assign numbers to the array @.*/
/*array indices are easier to use*/

call circleSort 1, # /*invoke circle sort subroutine. */
call circleSort 1, # /*invoke circle sort subroutine. */
y=@.1 /*start with the first element. */
y=@.1
do j=2 to #; y=y @.j; end /*assign array numbers to a list.*/
do j=2 to #; y=y @.j; end /*assign array numbers to a list.*/
/* [↑] recreate list from array.*/

say ' after sort: ' y /*display the sorted list of nums*/
say ' after sort: ' y /*display the sorted list of nums*/
exit /*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────CIRCLESORT subroutine───────────────*/
/*──────────────────────────────────CIRCLESORT subroutine───────────────*/
circleSort: procedure expose @.; parse arg lo,hi /*get the arguments.*/
circleSort: procedure expose @.; parse arg lo,hi /*get the arguments.*/
do while .circleSort(1,hi,0) \==0 /*invoke until done.*/
do while .circleSrt(1,hi,0) \== 0 /*invoke until done.*/
end /*while*/
end /*while*/ /*···short but sweet*/
return /*sort is complete. */
return /*circleSort is done*/
/*──────────────────────────────────.CIRCLESORT subroutine──────────────*/
/*──────────────────────────────────.CIRCLESRT subroutine───────────────*/
.circleSort: procedure expose @.; parse arg lo,hi,swaps /*get the args*/
.circleSrt: procedure expose @.; parse arg lo,hi,swaps /*get the args. */
if swaps=='' then swaps=0 /*assume 0 ? */
if swaps=='' then swaps=0 /*assume zero ? */
if lo==hi then return swaps /*we done ? */
if lo==hi then return swaps /*are we done ? */
high=hi; low=lo; mid=(hi-lo) % 2 /*assign vars.*/
high=hi; low=lo; mid=(hi-lo) % 2 /*assign indices*/
/* [↓] a section*/

do while lo<hi /*sort section*/
do while lo<hi /*sort section. */
if @.lo>@.hi then do /*out of ord ?*/
if @.lo>@.hi then call .swap lo,hi /*out of order ?*/
parse value @.lo @.hi with @.hi @.lo /*swap values.*/
lo=lo+1; hi=hi-1 /*add; subtract.*/
swaps=swaps+1 /*bump cntr. */
end /*while lo<hi*/ /*just 1 section*/
end
hip=hi+1 /*point to HI+1 */
lo=lo+1; hi=hi-1 /*bump or sub.*/
if lo==hi then if @.lo>@.hip then call .swap lo,hip /*out of order? */
swaps=.circleSrt(low,low+mid,swaps) /*sort lower. */
end /*while lo<hi*/
swaps=.circleSrt(low+mid+1,high,swaps) /* " higher, */

hip=hi+1 /*point: HI+1 */
return swaps /*section done. */
/*──────────────────────────────────.SWAP subroutine────────────────────*/
if lo==hi then if @.lo>@.hip then do /*out of ord? */
parse value @.lo @.hip with @.hip @.lo
.swap: arg a,b; parse value @.a @.b with @.b @.a; swaps=swaps+1; return</lang>
swaps=swaps+1 /*bump cntr. */
end
swaps=.circleSort(low,low+mid,swaps) /*sort lower. */
swaps=.circleSort(low+mid+1,high,swaps) /* " higher,*/
return swaps /*section done*/</lang>
'''output''' when using the default inputs:
'''output''' when using the default inputs:
<pre>
<pre>