Sorting Algorithms/Circle Sort: Difference between revisions

Content deleted Content added
→‎{{header|C}}: changed how middle element is handled; comments
→‎{{header|REXX}}: added the REXX language. -- ~~~~
Line 299:
print(L)
</lang>
 
=={{header|REXX}}==
<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
say 'before sort: ' x /* [↑] get numbers; use default?*/
#=words(x) /*obtain the number of elements. */
 
do i=1 for #; @.i=word(x,i); end /*assign numbers to the array @.*/
 
call circleSort 1, # /*invoke circle sort subroutine. */
y=@.1
do j=2 to #; y=y @.j; end /*assign array numbers to a list.*/
 
say ' after sort: ' y /*display the sorted list of nums*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────CIRCLESORT subroutine───────────────*/
circleSort: procedure expose @.; parse arg lo,hi /*get the arguments.*/
do while .circleSort(1,hi,0) \==0 /*invoke until done.*/
end /*while*/
return /*sort is complete. */
/*──────────────────────────────────.CIRCLESORT subroutine──────────────*/
.circleSort: procedure expose @.; parse arg lo,hi,swaps /*get the args*/
if swaps=='' then swaps=0 /*assume 0 ? */
if lo==hi then return swaps /*we done ? */
high=hi; low=lo; mid=(hi-lo) % 2 /*assign vars.*/
 
do while lo<hi /*sort section*/
if @.lo>@.hi then do /*out of ord ?*/
parse value @.lo @.hi with @.hi @.lo /*swap values.*/
swaps=swaps+1 /*bump cntr. */
end
lo=lo+1; hi=hi-1 /*bump or sub.*/
end /*while lo<hi*/
 
hip=hi+1 /*point: HI+1 */
if lo==hi then if @.lo>@.hip then do /*out of ord? */
parse value @.lo @.hip with @.hip @.lo
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:
<pre>
before sort: 6 7 8 9 2 5 3 4 1
 
after sort: 1 2 3 4 5 6 7 8 9
</pre>
 
=={{header|uBasic/4tH}}==