Sorting algorithms/Strand sort: Difference between revisions
Rename Perl 6 -> Raku, alphabetize, minor clean-up
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 1,105:
@a = strand_sort(@a);
say "After @a";</lang>
=={{header|Perl 6}}==▼
{{Works with|Rakudo|2018.04.01}}▼
<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 {▼
take do given @x[*-1] cmp @y[*-1] {▼
when More { pop @x }▼
when Less { pop @y }▼
when Same { pop(@x), pop(@y) }▼
}▼
}▼
}▼
sub strand (@x) {▼
my $i = 0;▼
my $prev = -Inf;▼
gather while $i < @x {▼
@x[$i] before $prev ?? $i++ !! take $prev = splice(@x, $i, 1)[0];▼
}▼
}▼
sub strand_sort (@x is copy) {▼
my @out;▼
@out M= strand(@x) while @x;▼
@out;▼
}▼
my @a = (^100).roll(10);▼
say "Before {@a}";▼
@a = strand_sort(@a);▼
say "After {@a}";▼
@a = <The quick brown fox jumps over the lazy dog>;▼
say "Before {@a}";▼
@a = strand_sort(@a);▼
say "After {@a}";</lang>▼
{{out}}▼
<pre>Before 1 20 64 72 48 75 96 55 42 74▼
After 1 20 42 48 55 64 72 74 75 96▼
Before The quick brown fox jumps over the lazy dog▼
After The brown dog fox jumps lazy over quick the</pre>▼
=={{header|Phix}}==
Line 1,511 ⟶ 1,467:
(strand-sort (build-list 10 (λ(_) (random 15))))
</lang>
(formerly Perl 6)
▲{{Works with|Rakudo|2018.04.01}}
▲<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 {
▲ take do given @x[*-1] cmp @y[*-1] {
▲ when More { pop @x }
▲ when Less { pop @y }
▲ when Same { pop(@x), pop(@y) }
▲ }
▲ }
▲}
▲sub strand (@x) {
▲ my $i = 0;
▲ my $prev = -Inf;
▲ gather while $i < @x {
▲ @x[$i] before $prev ?? $i++ !! take $prev = splice(@x, $i, 1)[0];
▲ }
▲}
▲sub strand_sort (@x is copy) {
▲ my @out;
▲ @out M= strand(@x) while @x;
▲ @out;
▲}
▲my @a = (^100).roll(10);
▲say "Before {@a}";
▲@a = strand_sort(@a);
▲say "After {@a}";
▲@a = <The quick brown fox jumps over the lazy dog>;
▲say "Before {@a}";
▲@a = strand_sort(@a);
▲say "After {@a}";</lang>
▲{{out}}
▲<pre>Before 1 20 64 72 48 75 96 55 42 74
▲After 1 20 42 48 55 64 72 74 75 96
▲Before The quick brown fox jumps over the lazy dog
▲After The brown dog fox jumps lazy over quick the</pre>
=={{header|REXX}}==
|