Sorting algorithms/Strand sort: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Mark as broken) |
SqrtNegInf (talk | contribs) 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) { |
||
my @x = | @x-in; |
|||
my @y = | @y-in; |
|||
flat @x, @y, |
|||
reverse gather while @x and @y { |
|||
when Increase { @x.shift } |
|||
⚫ | |||
when Decrease { @y.shift } |
|||
when More { pop @x } |
|||
when Less { pop @y } |
|||
} |
|||
when Same { pop(@x), pop(@y) } |
|||
} |
|||
⚫ | |||
take @x, @y; |
|||
} |
} |
||
} |
} |
||
sub strand (@x |
sub strand (@x) { |
||
⚫ | |||
my $prev = -Inf; |
my $prev = -Inf; |
||
⚫ | |||
gather while $i < @x { |
gather while $i < @x { |
||
@x[$i] before $prev ?? $i++ !! take $prev = splice(@x, $i, 1)[0]; |
|||
$i++; |
|||
⚫ | |||
else { |
|||
⚫ | |||
} |
|||
} |
} |
||
} |
} |
||
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 |
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 |