Sorting algorithms/Cocktail sort with shifting bounds: Difference between revisions
Sorting algorithms/Cocktail sort with shifting bounds (view source)
Revision as of 21:23, 10 May 2020
, 4 years ago→{{header|Raku}}: Add a Raku example
m (elided the __TOC__ statement from the task's epilogue.) |
Thundergnat (talk | contribs) (→{{header|Raku}}: Add a Raku example) |
||
Line 270:
{{"one","two","three","four"},{"four","one","three","two"}}
</pre>
=={{header|Raku}}==
{{works with|Rakudo|2020.05}}
<lang perl6>sub cocktail_sort ( @a ) {
my ($min, $max) = 0, +@a - 2;
loop {
my $swapped_forward = 0;
for $min .. $max -> $i {
given @a[$i] cmp @a[$i+1] {
when More {
@a[ $i, $i+1 ] .= reverse;
$swapped_forward = 1
}
default {}
}
}
last if not $swapped_forward;
$max -= 1;
my $swapped_backward = 0;
for ($min .. $max).reverse -> $i {
given @a[$i] cmp @a[$i+1] {
when More {
@a[ $i, $i+1 ] .= reverse;
$swapped_backward = 1
}
default {}
}
}
last if not $swapped_backward;
$min += 1;
}
@a
}
my @weights = (flat 0..9, 'A'..'F').roll(2 + ^4 .roll).join xx 100;
say @weights.sort.Str eq @weights.&cocktail_sort.Str ?? 'ok' !! 'not ok';</lang>
=={{header|REXX}}==
|