Sorting Algorithms/Circle Sort: Difference between revisions

Added Perl example
(Added Perl example)
Line 960:
end.
</lang>
 
=={{header|Perl}}==
Less flexible than the Perl 6 version, as written does only numeric comparisons.
{{trans|Perl 6}}
<lang perl>sub circlesort {
our @x; local *x = shift;
my($beg,$end) = @_;
 
my $swaps = 0;
if ($beg < $end) {
my $lo = $beg;
my $hi = $end;
while ($lo < $hi) {
if ($x[$lo] > $x[$hi]) { # 'gt' here for string comparison
@x[$lo,$hi] = @x[$hi,$lo];
++$swaps;
}
++$hi if --$hi == ++$lo
}
$swaps += circlesort(\@x, $beg, $hi);
$swaps += circlesort(\@x, $lo, $end);
}
$swaps;
}
 
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)) { say join ' ', @a }</lang>
{{out}}
<pre>-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
-99 -81 -78 -64 -29 -7 -1 16 20 25 26 35 36 39 46 59 76 85 88 100</pre>
 
=={{header|Perl 6}}==
2,392

edits