Jump to content

Sorting algorithms/Strand sort: Difference between revisions

m (→‎{{header|D}}: reuse sorted)
Line 733:
@a = strand_sort(@a);
say "After @a";</lang>
=={{header|Perl 6}}==
<lang perl6>sub merge(@x,@y) {
gather {
while @x and @y {
given @x[0] cmp @y[0] {
when Increase { take @x.shift }
when Decrease { take @y.shift }
when Same { take @x.shift, @y.shift }
}
}
take @x, @y;
}
}
 
sub strand(@x is rw) {
my $i = 0;
my $prev = @x[0];
gather while $i < @x {
if @x[$i] before $prev {
$i++;
}
else {
take $prev = splice(@x,$i,1)[0];
}
}
}
 
sub strand-sort(@x is copy) {
my @out;
@out = merge(@out, strand(@x)) while @x;
@out;
}
 
my @a = (^100).roll(20);
say "Before @a[]";
@a = strand-sort(@a);
say "After @a[]";
 
@a = <The quick brown fox jumped over the lazy dog>;
say "Before @a[]";
@a = strand-sort(@a);
say "After @a[]";</lang>
{{out}}
<pre>Before 48 78 93 80 81 32 40 78 41 54 8 58 39 96 2 35 0 39 93 61
After 0 2 8 32 35 39 39 40 41 48 54 58 61 78 78 80 81 93 93 96
Before The quick brown fox jumped over the lazy dog
After The brown dog fox jumped lazy over quick the</pre>
 
=={{header|PicoLisp}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.