Sorting Algorithms/Circle Sort: Difference between revisions

Content added Content deleted
m (removed nowiki)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 323: Line 323:
sample /sample sort .sample</lang>
sample /sample sort .sample</lang>

=={{header|Fortran}}==
=={{header|Fortran}}==
<lang fortran>
<lang fortran>
Line 793: Line 794:
{{out}}
{{out}}
<pre>1 2 3 4 5 6 7 8 9</pre>
<pre>1 2 3 4 5 6 7 8 9</pre>

=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>proc innerCircleSort[T](a: var openArray[T], lo, hi, swaps: int): int =
<lang nim>proc innerCircleSort[T](a: var openArray[T], lo, hi, swaps: int): int =
Line 1,045: Line 1,047:
-99 -81 -78 -64 -29 -7 -1 16 20 25 26 35 36 39 46 59 76 85 88 100
-99 -81 -78 -64 -29 -7 -1 16 20 25 26 35 36 39 46 59 76 85 88 100
-99 -81 -78 -64 -29 -7 -1 16 20 25 26 35 36 39 46 59 76 85 88 100</pre>
-99 -81 -78 -64 -29 -7 -1 16 20 25 26 35 36 39 46 59 76 85 88 100</pre>

=={{header|Perl 6}}==

The given algorithm can be simplified in several ways. There's no need to compute the midpoint, since the hi/lo will end up there. The extra swap conditional can be eliminated by incrementing hi at the correct moment inside the loop. There's no need to
pass accumulated swaps down the call stack.

This does generic comparisons, so it works on any ordered type, including numbers or strings.
<lang perl6>sub circlesort (@x, $beg, $end) {
my $swaps = 0;
if $beg < $end {
my ($lo, $hi) = $beg, $end;
repeat {
if @x[$lo] after @x[$hi] {
@x[$lo,$hi] .= reverse;
++$swaps;
}
++$hi if --$hi == ++$lo
} while $lo < $hi;
$swaps += circlesort(@x, $beg, $hi);
$swaps += circlesort(@x, $lo, $end);
}
$swaps;
}

say my @x = (-100..100).roll(20);
say @x while circlesort(@x, 0, @x.end);

say @x = <The quick brown fox jumps over the lazy dog.>;
say @x while circlesort(@x, 0, @x.end);</lang>
{{out}}
<pre>16 35 -64 -29 46 36 -1 -99 20 100 59 26 76 -78 39 85 -7 -81 25 88
-99 -78 16 20 36 -81 -29 46 25 59 -64 -7 39 26 88 -1 35 85 76 100
-99 -78 -29 -81 16 -64 -7 20 -1 39 25 26 36 46 59 35 76 88 85 100
-99 -81 -78 -64 -29 -7 -1 16 20 25 26 35 36 39 46 59 76 85 88 100
The quick brown fox jumps over the lazy dog.
The brown fox jumps lazy dog. quick over the
The brown dog. fox jumps lazy over quick the</pre>


=={{header|Phix}}==
=={{header|Phix}}==
Line 1,280: Line 1,245:
'#("cat" "chair" "sponge" "table")
'#("cat" "chair" "sponge" "table")
</pre>
</pre>

=={{header|Raku}}==
(formerly Perl 6)

The given algorithm can be simplified in several ways. There's no need to compute the midpoint, since the hi/lo will end up there. The extra swap conditional can be eliminated by incrementing hi at the correct moment inside the loop. There's no need to
pass accumulated swaps down the call stack.

This does generic comparisons, so it works on any ordered type, including numbers or strings.
<lang perl6>sub circlesort (@x, $beg, $end) {
my $swaps = 0;
if $beg < $end {
my ($lo, $hi) = $beg, $end;
repeat {
if @x[$lo] after @x[$hi] {
@x[$lo,$hi] .= reverse;
++$swaps;
}
++$hi if --$hi == ++$lo
} while $lo < $hi;
$swaps += circlesort(@x, $beg, $hi);
$swaps += circlesort(@x, $lo, $end);
}
$swaps;
}

say my @x = (-100..100).roll(20);
say @x while circlesort(@x, 0, @x.end);

say @x = <The quick brown fox jumps over the lazy dog.>;
say @x while circlesort(@x, 0, @x.end);</lang>
{{out}}
<pre>16 35 -64 -29 46 36 -1 -99 20 100 59 26 76 -78 39 85 -7 -81 25 88
-99 -78 16 20 36 -81 -29 46 25 59 -64 -7 39 26 88 -1 35 85 76 100
-99 -78 -29 -81 16 -64 -7 20 -1 39 25 26 36 46 59 35 76 88 85 100
-99 -81 -78 -64 -29 -7 -1 16 20 25 26 35 36 39 46 59 76 85 88 100
The quick brown fox jumps over the lazy dog.
The brown fox jumps lazy dog. quick over the
The brown dog. fox jumps lazy over quick the</pre>


=={{header|REXX}}==
=={{header|REXX}}==
Line 1,418: Line 1,421:
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]

=={{header|Scala}}==
=={{header|Scala}}==
<lang Scala>object CircleSort extends App {
<lang Scala>object CircleSort extends App {