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 /* |
say 'before sort: ' x /*display the unsorted list of #s*/ |
||
#=words(x) /*obtain the number of elements. */ |
#=words(x) /*obtain the number of elements. */ |
||
⚫ | |||
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 @.*/ |
||
⚫ | |||
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.*/ |
||
⚫ | |||
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 . |
do while .circleSrt(1,hi,0) \== 0 /*invoke until done.*/ |
||
end /*while*/ |
end /*while*/ /*···short but sweet*/ |
||
return /* |
return /*circleSort is done*/ |
||
/*──────────────────────────────────. |
/*──────────────────────────────────.CIRCLESRT subroutine───────────────*/ |
||
. |
.circleSrt: procedure expose @.; parse arg lo,hi,swaps /*get the args. */ |
||
if swaps=='' then swaps=0 |
if swaps=='' then swaps=0 /*assume zero ? */ |
||
if lo==hi then return swaps |
if lo==hi then return swaps /*are we done ? */ |
||
high=hi; low=lo; mid=(hi-lo) % 2 |
high=hi; low=lo; mid=(hi-lo) % 2 /*assign indices*/ |
||
/* [↓] a section*/ |
|||
do while lo<hi /*sort section. */ |
|||
if @.lo>@.hi then call .swap lo,hi /*out of order ?*/ |
|||
lo=lo+1; hi=hi-1 /*add; subtract.*/ |
|||
end /*while lo<hi*/ /*just 1 section*/ |
|||
hip=hi+1 /*point to HI+1 */ |
|||
if lo==hi then if @.lo>@.hip then call .swap lo,hip /*out of order? */ |
|||
⚫ | |||
end /*while lo<hi*/ |
|||
⚫ | |||
return swaps /*section done. */ |
|||
/*──────────────────────────────────.SWAP subroutine────────────────────*/ |
|||
if lo==hi then if @.lo>@.hip then do /*out of ord? */ |
|||
.swap: arg a,b; parse value @.a @.b with @.b @.a; swaps=swaps+1; return</lang> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
'''output''' when using the default inputs: |
'''output''' when using the default inputs: |
||
<pre> |
<pre> |