Sorting Algorithms/Circle Sort: Difference between revisions

Content deleted Content added
m →‎{{header|REXX}}: simplified main subroutine.
m →‎{{header|REXX}}: made subroutines to simplify making of array, list.
Line 303:
<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 /*use the defaults ? */
saycall make@ 'before sort: ' x /*display the unsorted list, make ofan #sarray*/
#=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 @.*/
/*array indices are easier to use*/
call circleSort # /*invoke circle sort subroutine. */
y=@.1call makeY 'before sort:' /*make a list, /*start withdisplay the first elementlist. */
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*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────CIRCLESORT subroutine───────────────*/
Line 329 ⟶ 322:
swaps=.circleSrt(low+mid+1, high, swaps) /* " higher, */
return swaps /*section done. */
/*──────────────────────────────────one─liner subroutines───────────────*/
/*──────────────────────────────────.SWAP subroutine────────────────────*/
.swap: arg a,b; parse value @.a @.b with @.b @.a; swaps=swaps+1; return</lang>
make@: #=words(x); do i=1 for #; @.i=word(x,i); end; /*assignsay numbersarg(1) to thex; array @.*/return
'''output''' when using the default inputs:
makeY: y=@.1; do j=2 to #; y=y @.j; end; say arg(1) y; return</lang>
'''output''' when using the default inputsinput:
<pre>
before sort: 6 7 8 9 2 5 3 4 1