Sorting Algorithms/Circle Sort: Difference between revisions

Content added Content deleted
(Added Julia language)
m (→‎{{header|REXX}}: optimized a subroutine, altered capitalization, changed an output text, added templates for the output.)
Line 980: Line 980:
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
circleSort: do while .circleSrt(1, arg(1), 0)\==0; end; return
circleSort: do while .circleSrt(1, arg(1), 0)\==0; end; return
make_array: #=words(x); do i=1 for #; @.i=word(x, i); end; say arg(1) x; return
make_array: #=words(x); do i=1 for #; @.i=word(x, i); end; say arg(1) x; return
make_list: y=@.1; do j=2 to #; y=y @.j; end; say arg(1) y; return
make_list: y=@.1; do j=2 for #-1; y=y @.j; end; say arg(1) y; return
.swap: parse arg a,b; parse value @.a @.b with @.b @.a; swaps=swaps+1; return
.swap: parse arg a,b; parse value @.a @.b with @.b @.a; swaps=swaps+1; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
.circleSrt: procedure expose @.; parse arg lo,hi,swaps /*obtain LO & HI arguments.*/
.circleSrt: procedure expose @.; parse arg LO,HI,swaps /*obtain LO & HI arguments.*/
if lo==hi then return swaps /*1 element? Done with sort.*/
if LO==HI then return swaps /*1 element? Done with sort.*/
high=hi; low=lo; mid=(hi-lo) % 2 /*assign some indices. */
high=HI; low=LO; mid=(HI-LO) % 2 /*assign some indices. */
/* [↓] sort a section of #'s*/
/* [↓] sort a section of #'s*/
do while lo<hi /*sort within a section. */
do while LO<HI /*sort within a section. */
if @.lo>@.hi then call .swap lo,hi /*are numbers out of order ? */
if @.LO>@.HI then call .swap LO,HI /*are numbers out of order ? */
lo=lo+1; hi=hi-1 /*add to LO; shrink the HI. */
LO=LO+1; HI=HI-1 /*add to LO; shrink the HI. */
end /*while ··· */ /*just process one section. */
end /*while*/ /*just process one section. */
_=hi+1 /*point to HI plus one. */
_=hi+1 /*point to HI plus one. */
if lo==hi & @.lo>@._ then call .swap lo, _ /*numbers still out of order?*/
if LO==HI & @.LO>@._ then call .swap LO, _ /*numbers still out of order?*/
swaps=.circleSrt(low, low+mid, swaps) /*sort the lower section. */
swaps=.circleSrt(low, low+mid, swaps) /*sort the lower section. */
swaps=.circleSrt(low+mid+1, high, swaps) /* " " higher " */
swaps=.circleSrt(low+mid+1, high, swaps) /* " " higher " */
return swaps /*the section sorting is done*/</lang>
return swaps /*the section sorting is done*/</lang>
'''output''' &nbsp; when using the default input:
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
before sort: 6 7 8 9 2 5 3 4 1
before sort: 6 7 8 9 2 5 3 4 1
after sort: 1 2 3 4 5 6 7 8 9
after sort: 1 2 3 4 5 6 7 8 9
</pre>
</pre>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 2 3 3 5 5 1 1 7 7 6 6 4 4 0 0 </tt>
{{out|output|text=&nbsp; when using the input of: &nbsp; <tt> 2 3 3 5 5 1 1 7 7 6 6 4 4 0 0 </tt>}}
<pre>
<pre>
before sort: 2 3 3 5 5 1 1 7 7 6 6 4 4 0 0
before sort: 2 3 3 5 5 1 1 7 7 6 6 4 4 0 0
after sort: 0 0 1 1 2 3 3 4 4 5 5 6 6 7 7
after sort: 0 0 1 1 2 3 3 4 4 5 5 6 6 7 7
</pre>
</pre>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 2 3 44 44 5.77 +1 -12345 -3 -3.9 1e7 9 </tt>
{{out|output|text=&nbsp; when using the input of: &nbsp; <tt> 2 3 44 44 5.77 +1 -12345 -3 -3.9 1e7 9 </tt>}}
<pre>
<pre>
before sort: 2 3 44 44 5.77 +1 -12345 -3 -3.9 1e7 0
before sort: 2 3 44 44 5.77 +1 -12345 -3 -3.9 1e7 0
before sort: -12345 -3.9 -3 0 +1 2 3 5.77 44 44 1e7
after sort: -12345 -3.9 -3 0 +1 2 3 5.77 44 44 1e7
</pre>
</pre>
'''output''' &nbsp; when using the input of: &nbsp; <tt> assinine donkey bovine cattle canine dog corvine crow equine horse feline cat hircine goat leporine hare lupine wolf murine rodent piscine fish porcine pig ursine bear vulpine fox </tt>
'''output''' &nbsp; when using the input of: &nbsp; <tt> assinine donkey bovine cattle canine dog corvine crow equine horse feline cat hircine goat leporine hare lupine wolf murine rodent piscine fish porcine pig ursine bear vulpine fox </tt>