Sorting Algorithms/Circle Sort: Difference between revisions

m
(Added solution for Action!)
m (→‎{{header|Wren}}: Minor tidy)
 
(5 intermediate revisions by 5 users not shown)
Line 51:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F circle_sort_backend(&A, Int l, r)
V n = r - l
I n < 2
Line 83:
print(l.len)
print(n)
print(l)</langsyntaxhighlight>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program circleSort64.s */
Line 294:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
<pre>
Display table before sort.
Line 324:
=={{header|Action!}}==
Action! language does not support recursion. Therefore an iterative approach with a stack has been proposed.
<langsyntaxhighlight Actionlang="action!">DEFINE MAX_COUNT="100"
INT ARRAY stack(MAX_COUNT)
INT stackSize
Line 434:
Test(c,8)
Test(d,12)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Circle_Sort.png Screenshot from Atari 8-bit computer]
Line 461:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program circleSort.s */
Line 656:
.include "../affichage.inc"
 
</syntaxhighlight>
</lang>
<pre>
Display table before sort.
Line 677:
Table sorted.
</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">innerCircleSort: function [ar, lo, hi, swaps][
localSwaps: swaps
localHi: hi
localLo: lo
if localLo = localHi -> return swaps
 
high: localHi
low: localLo
mid: (localHi - localLo) / 2
 
while [localLo < localHi] [
if ar\[localLo] > ar\[localHi] [
tmp: ar\[localLo]
ar\[localLo]: ar\[localHi]
ar\[localHi]: tmp
localSwaps: localSwaps + 1
]
localLo: localLo + 1
localHi: localHi - 1
]
if localLo = localHi [
if ar\[localLo] > ar\[localHi + 1] [
tmp: ar\[localLo]
ar\[localLo]: ar\[localHi + 1]
ar\[localHi + 1]: tmp
localSwaps: localSwaps + 1
]
]
 
localSwaps: innerCircleSort ar low low + mid localSwaps
localSwaps: innerCircleSort ar low + mid + 1 high localSwaps
return localSwaps
]
 
circleSort: function [arr][
result: new arr
while [not? zero? innerCircleSort result 0 dec size result 0][]
return result
]
 
print circleSort [3 1 2 8 5 7 9 4 6]</syntaxhighlight>
 
{{out}}
 
<pre>1 2 3 4 5 6 7 8 9</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">nums := [6, 7, 8, 9, 2, 5, 3, 4, 1]
while circlesort(nums, 1, nums.Count(), 0) ; 1-based
continue
Line 709 ⟶ 756:
swaps := circlesort(Arr, low+mid+1, high, swaps)
return swaps
}</langsyntaxhighlight>
{{out}}
<pre>[1, 2, 3, 4, 5, 6, 7, 8, 9]</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
 
int circle_sort_inner(int *start, int *end)
Line 747 ⟶ 794:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 756 ⟶ 803:
 
=={{header|C#}}==
<langsyntaxhighlight lang="c#">using System;
using System.Linq;
 
Line 816 ⟶ 863:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 825 ⟶ 872:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
 
int circlesort(int* arr, int lo, int hi, int swaps) {
Line 871 ⟶ 918:
circlesortDriver(arr, sizeof(arr)/sizeof(int));
return 0;
}</langsyntaxhighlight>
Output:
<pre>6 7 8 9 2 5 3 4 1
Line 878 ⟶ 925:
 
=={{header|CoffeeScript}}==
<syntaxhighlight lang="text">circlesort = (arr, lo, hi, swaps) ->
if lo == hi
return (swaps)
Line 910 ⟶ 957:
 
while circlesort(VA,0,VA.length-1,0)
console.log VA</langsyntaxhighlight>
Output:
<pre>console: -1,1,0,3,4,5,8,12,2,9,6,10,7,13,11,14
Line 917 ⟶ 964:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.array, std.traits;
 
void circlesort(T)(T[] items) if (isMutable!T) {
Line 966 ⟶ 1,013:
assert(data.isSorted);
}
}</langsyntaxhighlight>
{{out}}
<pre>[-4, -1, 0, 1, 2, 3, 5, 6, 8, 101]</pre>
Line 972 ⟶ 1,019:
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Sorting_Algorithms;
 
Line 1,042 ⟶ 1,089:
end;
readln;
end.</langsyntaxhighlight>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Sort do
def circle_sort(data) do
List.to_tuple(data)
Line 1,087 ⟶ 1,134:
data = [6, 7, 8, 9, 2, 5, 3, 4, 1]
IO.puts "before sort: #{inspect data}"
IO.puts " after sort: #{inspect Sort.circle_sort(data)}"</langsyntaxhighlight>
 
{{out}}
Line 1,097 ⟶ 1,144:
=={{header|Forth}}==
This one features the newest version of the algorithm on [http://sourceforge.net/p/forth-4th/wiki/Circle%20sort/ Sourceforge].
<syntaxhighlight lang="text">[UNDEFINED] cell- [IF] : cell- 1 cells - ; [THEN]
 
defer precedes ( addr addr -- flag )
Line 1,129 ⟶ 1,176:
: .sample sample /sample cells bounds do i ? 1 cells +loop ;
sample /sample sort .sample</langsyntaxhighlight>
 
=={{header|Fortran}}==
<langsyntaxhighlight lang="fortran">
!
module circlesort
Line 1,209 ⟶ 1,256:
end program sort
 
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 21-10-2016
' compile with: fbc -s console
' for boundry checks on array's compile with: fbc -s console -exx
Line 1,276 ⟶ 1,323:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>unsorted -4 -1 1 0 5 -7 -2 4 -6 -3 2 6 3 7 -5
Line 1,282 ⟶ 1,329:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,323 ⟶ 1,370:
fmt.Printf("Sorted : %v\n\n", a)
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,338 ⟶ 1,385:
This code uses the tortoise-and-the-hare technique to split the input list in two and compare the relevant positions.
 
<langsyntaxhighlight lang="haskell">import Data.Bool (bool)
 
circleSort :: Ord a => [a] -> [a]
Line 1,358 ⟶ 1,405:
swap d x (s,y:ys,ls,rs)
| bool (<=) (<) d x y = ( d || s,ys,x:ls,y:rs)
| otherwise = (not d || s,ys,y:ls,x:rs)</langsyntaxhighlight>
 
{{out}}
Line 1,372 ⟶ 1,419:
Of more parsing and atomic data, or less parsing with large data groups the latter produces faster J programs. Consequently each iteration laminates the original with its reverse. It joins the recursive call to the pairwise minima of the left block to the recursive call of the pairwise maxima of the right block, repeating the operations while the output changes. This is sufficient for power of 2 length data. The pre verb adjusts the data length. And post recovers the original data. This implementation discards the "in place" property described at the sourceforge link.
 
<syntaxhighlight lang="j">
<lang J>
circle_sort =: post power_of_2_length@pre NB. the main sorting verb
power_of_2_length =: even_length_iteration^:_ NB. repeat while the answer changes
Line 1,378 ⟶ 1,425:
pre =: , (-~ >.&.(2&^.))@# # >./ NB. extend data to next power of 2 length
post =: ({.~ #)~ NB. remove the extra data
</syntaxhighlight>
</lang>
Examples:
<syntaxhighlight lang="j">
<lang J>
show =: [ smoutput
 
Line 1,398 ⟶ 1,445:
│0 0 1 1 2 3 3 4 4 5 5 6 6 7 7│0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12│
└─────────────────────────────┴──────────────────────────────────────────────────────┘
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.Arrays;
 
public class CircleSort {
Line 1,449 ⟶ 1,496:
arr[idx2] = tmp;
}
}</langsyntaxhighlight>
 
<pre>[2, 14, 4, 6, 8, 1, 3, 5, 7, 11, 0, 13, 12, -1]
Line 1,462 ⟶ 1,509:
 
"circlesort" as defined in this section can be used to sort any JSON array. In case your jq does not have "until" as a builtin, here is its definition:
<langsyntaxhighlight lang="jq">def until(cond; next):
def _until: if cond then . else (next|_until) end;
_until;</langsyntaxhighlight>
<langsyntaxhighlight lang="jq">def circlesort:
 
def swap(i;j): .[i] as $t | .[i] = .[j] | .[j] = $t;
Line 1,498 ⟶ 1,545:
| if $swaps == 0 then .
else circlesort
end ;</langsyntaxhighlight>
'''Example:'''
<langsyntaxhighlight lang="jq">"The quick brown fox jumps over the lazy dog" | split(" ") | circlesort</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang="sh">$ jq -n -c -f -M circleSort.jq
["The","brown","dog","fox","jumps","lazy","over","quick","the"]</langsyntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">function _ciclesort!(arr::Vector, lo::Int, hi::Int, swaps::Int)
lo == hi && return swaps
high = hi
Line 1,539 ⟶ 1,586:
 
v = rand(10)
println("# $v\n -> ", ciclesort!(v))</langsyntaxhighlight>
 
{{out}}
Line 1,546 ⟶ 1,593:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.0
 
fun<T: Comparable<T>> circleSort(array: Array<T>, lo: Int, hi: Int, nSwaps: Int): Int {
Line 1,589 ⟶ 1,636:
while (circleSort(array2, 0, array2.size - 1, 0) != 0) ;
println("Sorted : ${array2.asList()}")
}</langsyntaxhighlight>
 
{{out}}
Line 1,602 ⟶ 1,649:
=={{header|Lua}}==
The first argument to the 'inner' function needs to be a reference to the table as Lua cannot use a pointer to the first element's memory address. Conversely the 'outer' function only needs one argument as the size of the table is innately knowable.
<langsyntaxhighlight Lualang="lua">-- Perform one iteration of a circle sort
function innerCircle (t, lo, hi, swaps)
if lo == hi then return swaps end
Line 1,633 ⟶ 1,680:
local array = {6, 7, 8, 9, 2, 5, 3, 4, 1}
circleSort(array)
print(table.concat(array, " "))</langsyntaxhighlight>
{{out}}
<pre>1 2 3 4 5 6 7 8 9</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[CircleSort, NestedCircleSort]
CircleSort[d_List, l_, h_] :=
Module[{high, low, mid, lo = l, hi = h, data = d},
Line 1,672 ⟶ 1,719:
NestedCircleSort[Echo@{2, 1}]
NestedCircleSort[Echo@{1}]
NestedCircleSort[Echo@{}]</langsyntaxhighlight>
{{out}}
<pre>>>{6,7,8,9,2,5,3,4,1}
Line 1,696 ⟶ 1,743:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">proc innerCircleSort[T](a: var openArray[T], lo, hi, swaps: int): int =
var localSwaps: int = swaps
var localHi: int = hi
Line 1,732 ⟶ 1,779:
echo "Original: ", $arr[i]
arr[i].circleSort()
echo "Sorted: ", $arr[i], if i != arr.high: "\n" else: ""</langsyntaxhighlight>
 
{{out}}
Line 1,743 ⟶ 1,790:
=={{header|Objeck}}==
{{trans|Objeck}}
<langsyntaxhighlight lang="objeck">class CircleSort {
function : Main(args : String[]) ~ Nil {
circleSort([2, 14, 4, 6, 8, 1, 3, 5, 7, 11, 0, 13, 12, -1]);
Line 1,793 ⟶ 1,840:
}
}
</syntaxhighlight>
</lang>
 
Output:
Line 1,805 ⟶ 1,852:
=={{header|PARI/GP}}==
This follows the pseudocode pretty closely.
<langsyntaxhighlight lang="parigp">circlesort(v)=
{
local(v=v); \\ share with cs
Line 1,830 ⟶ 1,877:
}
print(example=[6,7,8,9,2,5,3,4,1]);
print(circlesort(example));</langsyntaxhighlight>
{{out}}
<pre>[6, 7, 8, 9, 2, 5, 3, 4, 1]
Line 1,836 ⟶ 1,883:
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">
{
source file name on linux is ./p.p
Line 1,914 ⟶ 1,961:
writeln();
end.
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
Less flexible than the Raku version, as written does only numeric comparisons.
{{trans|Raku}}
<langsyntaxhighlight lang="perl">sub circlesort {
our @x; local *x = shift;
my($beg,$end) = @_;
Line 1,941 ⟶ 1,988:
 
my @a = <16 35 -64 -29 46 36 -1 -99 20 100 59 26 76 -78 39 85 -7 -81 25 88>;
while (circlesort(\@a, 0, $#a)) { print join(' ', @a), "\n" }</langsyntaxhighlight>
{{out}}
<pre>-99 -78 16 20 36 -81 -29 46 25 59 -64 -7 39 26 88 -1 35 85 76 100
Line 1,949 ⟶ 1,996:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 1,992 ⟶ 2,039:
--array = shuffle(deep_copy(array))</span>
<span style="color: #000000;">circle_sort</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{out}}
Shows the full inner workings: call depth and range being considered, after each swap made.
Line 2,021 ⟶ 2,068:
=={{header|Python}}==
The doctest passes with odd and even length lists. As do the random tests. Please see circle_sort.__doc__ for example use and output.
<langsyntaxhighlight lang="python">
#python3
#tests: expect no output.
Line 2,075 ⟶ 2,122:
print(N)
print(L)
</syntaxhighlight>
</lang>
 
=={{header|Quackery}}==
Line 2,086 ⟶ 2,133:
 
 
<langsyntaxhighlight Quackerylang="quackery"> [ dup size 2 < iff
[ drop true ] done
true swap
Line 2,133 ⟶ 2,180:
$ "bababadalgharaghtakamminarronnkonnbronntonnerronntuonnthunntrovarrhounawnskawntoohoohoordenenthurnuk"
dup echo$ cr
circlesort echo$ cr</langsyntaxhighlight>
 
{{out}}
Line 2,147 ⟶ 2,194:
(diadic) function can be used to compare... e.g. <code>string&lt;?</code>.
 
<langsyntaxhighlight lang="racket">#lang racket
(define (circle-sort v0 [<? <])
(define v (vector-copy v0))
Line 2,181 ⟶ 2,228:
(for ([_ 10]) (sort-random-vector))
 
(circle-sort '#("table" "chair" "cat" "sponge") string<?)</langsyntaxhighlight>
 
{{out}}
Line 2,225 ⟶ 2,272:
 
This does generic comparisons, so it works on any ordered type, including numbers or strings.
<syntaxhighlight lang="raku" perl6line>sub circlesort (@x, $beg, $end) {
my $swaps = 0;
if $beg < $end {
Line 2,246 ⟶ 2,293:
 
say @x = <The quick brown fox jumps over the lazy dog.>;
say @x while circlesort(@x, 0, @x.end);</langsyntaxhighlight>
{{out}}
<pre>16 35 -64 -29 46 36 -1 -99 20 100 59 26 76 -78 39 85 -7 -81 25 88
Line 2,259 ⟶ 2,306:
This REXX version will work with any numbers that REXX supports, including negative and/or floating point numbers;
<br>it also will work with character strings.
<langsyntaxhighlight lang="rexx">/*REXX program uses a circle sort algorithm to sort an array (or list) of numbers. */
parse arg x /*obtain optional arguments from the CL*/
if x='' | x="," then x= 6 7 8 9 2 5 3 4 1 /*Not specified? Then use the default.*/
Line 2,284 ⟶ 2,331:
swaps= .circleSrt(low, low+mid, swaps) /*sort the lower section. */
swaps= .circleSrt(low+mid+1, high, swaps) /* " " higher " */
return swaps /*the section sorting is done*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 2,307 ⟶ 2,354:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Sorting Algorithms/Circle Sort
 
Line 2,352 ⟶ 2,399:
see svect
see "]" + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,359 ⟶ 2,406:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">class Array
def circle_sort!
while _circle_sort!(0, size-1) > 0
Line 2,389 ⟶ 2,436:
ary = [6, 7, 8, 9, 2, 5, 3, 4, 1]
puts "before sort: #{ary}"
puts " after sort: #{ary.circle_sort!}"</langsyntaxhighlight>
 
{{out}}
Line 2,396 ⟶ 2,443:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn _circle_sort<T: PartialOrd>(a: &mut [T], low: usize, high: usize, swaps: usize) -> usize {
if low == high {
return swaps;
Line 2,437 ⟶ 2,484:
circle_sort(&mut v);
println!("after: {:?}", v);
}</langsyntaxhighlight>
 
{{out}}
Line 2,446 ⟶ 2,493:
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object CircleSort extends App {
 
def sort(arr: Array[Int]): Array[Int] = {
Line 2,485 ⟶ 2,532:
println(sort(Array[Int](2, 14, 4, 6, 8, 1, 3, 5, 7, 11, 0, 13, 12, -1)).mkString(", "))
 
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func circlesort(arr, beg=0, end=arr.end) {
var swaps = 0
if (beg < end) {
Line 2,509 ⟶ 2,556:
 
var strs = ["John", "Kate", "Zerg", "Alice", "Joe", "Jane", "Alice"]
do { say strs } while circlesort(strs)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,522 ⟶ 2,569:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">func circleSort<T: Comparable>(_ array: inout [T]) {
func circSort(low: Int, high: Int, swaps: Int) -> Int {
if low == high {
Line 2,560 ⟶ 2,607:
print("before: \(array2)")
circleSort(&array2)
print(" after: \(array2)")</langsyntaxhighlight>
 
{{out}}
Line 2,572 ⟶ 2,619:
=={{header|uBasic/4tH}}==
This one uses the optimized version featured at [http://sourceforge.net/p/forth-4th/wiki/Circle%20sort/ Sourceforge].
<syntaxhighlight lang="text">PRINT "Circle sort:"
n = FUNC (_InitArray)
PROC _ShowArray (n)
Line 2,629 ⟶ 2,676:
 
PRINT
RETURN</langsyntaxhighlight>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="vlang">fn circle_sort(mut a []int, l int, h int, s int) int {
mut hi := h
mut lo := l
mut swaps := s
if lo == hi {
return swaps
}
high, low := hi, lo
mid := (hi - lo) / 2
for lo < hi {
if a[lo] > a[hi] {
a[lo], a[hi] = a[hi], a[lo]
swaps++
}
lo++
hi--
}
if lo == hi {
if a[lo] > a[hi+1] {
a[lo], a[hi+1] = a[hi+1], a[lo]
swaps++
}
}
swaps = circle_sort(mut a, low, low+mid, swaps)
swaps = circle_sort(mut a, low+mid+1, high, swaps)
return swaps
}
fn main() {
aa := [
[6, 7, 8, 9, 2, 5, 3, 4, 1],
[2, 14, 4, 6, 8, 1, 3, 5, 7, 11, 0, 13, 12, -1],
]
for a1 in aa {
mut a:=a1.clone()
println("Original: $a")
for circle_sort(mut a, 0, a.len-1, 0) != 0 {
// empty block
}
println("Sorted : $a\n")
}
}</syntaxhighlight>
 
{{out}}
<pre>
Original: [6, 7, 8, 9, 2, 5, 3, 4, 1]
Sorted : [1, 2, 3, 4, 5, 6, 7, 8, 9]
 
Original: [2, 14, 4, 6, 8, 1, 3, 5, 7, 11, 0, 13, 12, -1]
Sorted : [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 14]
</pre>
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">var circleSort // recursive
circleSort = Fn.new { |a, lo, hi, swaps|
if (lo == hi) return swaps
Line 2,661 ⟶ 2,762:
}
 
var asarray = [ [6, 7, 8, 9, 2, 5, 3, 4, 1], [2, 14, 4, 6, 8, 1, 3, 5, 7, 11, 0, 13, 12, -1] ]
for (a in asarray) {
System.print("Before: %(a)")
while (circleSort.call(a, 0, a.count-1, 0) != 0) {}
System.print("After : %(a)")
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 2,676 ⟶ 2,777:
Before: [2, 14, 4, 6, 8, 1, 3, 5, 7, 11, 0, 13, 12, -1]
After : [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 14]
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">int Array;
 
func CircleSort(Lo, Hi, Swaps);
int Lo, Hi, Swaps;
int Low, High, Mid, T;
[if Lo = Hi then return Swaps;
Low:= Lo;
High:= Hi;
Mid:= (Hi-Lo)/2;
while Lo < Hi do
[if Array(Lo) > Array(Hi) then
[T:= Array(Lo); Array(Lo):= Array(Hi); Array(Hi):= T;
Swaps:= Swaps+1;
];
Lo:= Lo+1;
Hi:= Hi-1;
];
if Lo = Hi then
if Array(Lo) > Array(Hi+1) then
[T:= Array(Lo); Array(Lo):= Array(Hi+1); Array(Hi+1):= T;
Swaps:= Swaps+1;
];
Swaps:= CircleSort(Low, Low+Mid, Swaps);
Swaps:= CircleSort(Low+Mid+1, High, Swaps);
return Swaps;
];
 
int I;
[Array:= [5, -1, 101, -4, 0, 1, 8, 6, 2, 3];
while CircleSort(0, 10-1, 0) # 0 do [];
for I:= 0 to 10-1 do
[IntOut(0, Array(I)); ChOut(0, ^ )];
]</syntaxhighlight>
{{out}}
<pre>
-4 -1 0 1 2 3 5 6 8 101
</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn circleSort(list){
csort:=fcn(list,lo,hi,swaps){
if(lo==hi) return(swaps);
Line 2,702 ⟶ 2,842:
while(csort(list,0,list.len()-1,0)){ list.println() }
list
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">circleSort(L(6,7,8,9,2,5,3,4,1));
circleSort(L(5,-1,101,-4,0,1,8,6,2,3));</langsyntaxhighlight>
{{out}}
<pre>
Line 2,722 ⟶ 2,862:
This version of Circle sort was based on the optimized version on [http://sourceforge.net/p/forth-4th/wiki/Circle%20sort/ Sourceforge]. It will also show a few asterisks while running, because it will take some time to finish (about two minutes).
 
<langsyntaxhighlight lang="zxbasic">
10 DIM a(100): DIM s(32): RANDOMIZE : LET p=1: GO SUB 3000: GO SUB 2000: GO SUB 4000
20 STOP
Line 2,734 ⟶ 2,874:
3000 FOR x=1 TO 100: LET a(x)=RND: NEXT x: RETURN
4000 FOR x=1 TO 100: PRINT x,a(x): NEXT x: RETURN
</syntaxhighlight>
</lang>
9,476

edits