Sorting algorithms/Strand sort: Difference between revisions

Content added Content deleted
(→‎{{header|Perl 6}}: Mark as broken)
m (→‎{{header|Perl 6}}: fixed merge routine)
Line 1,072: Line 1,072:


=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{Works with|Rakudo|2018.04.01}}
{{broken|Perl 6}}
<lang perl6>sub infix:<M> (@x, @y) {
<lang perl6>sub infix:<M> (@x-in, @y-in) {
gather {
my @x = | @x-in;
while @x and @y {
my @y = | @y-in;
take do given @x[0] cmp @y[0] {
flat @x, @y,
reverse gather while @x and @y {
when Increase { @x.shift }
take do given @x[*-1] cmp @y[*-1] {
when Decrease { @y.shift }
when Same { @x.shift, @y.shift }
when More { pop @x }
when Less { pop @y }
}
when Same { pop(@x), pop(@y) }
}
}
take @x, @y;
}
}
}
}


sub strand (@x is rw) {
sub strand (@x) {
my $i = 0;
my $prev = -Inf;
my $prev = -Inf;
my $i = 0;
gather while $i < @x {
gather while $i < @x {
if @x[$i] before $prev {
@x[$i] before $prev ?? $i++ !! take $prev = splice(@x, $i, 1)[0];
$i++;
}
else {
take $prev = splice(@x, $i, 1)[0];
}
}
}
}
}
Line 1,106: Line 1,101:


my @a = (^100).roll(10);
my @a = (^100).roll(10);
say "Before @a[]";
say "Before {@a}";
@a = strand_sort(@a);
@a = strand_sort(@a);
say "After @a[]";
say "After {@a}";


@a = <The quick brown fox jumps over the lazy dog>;
@a = <The quick brown fox jumps over the lazy dog>;
say "Before @a[]";
say "Before {@a}";
@a = strand_sort(@a);
@a = strand_sort(@a);
say "After @a[]";</lang>
say "After {@a}";</lang>
{{out}}
{{out}}
<pre>Before 1 20 64 72 48 75 96 55 42 74
<pre>Before 1 20 64 72 48 75 96 55 42 74