Sorting algorithms/Strand sort: Difference between revisions

m
→‎{{header|Perl}}: removed deprecated given/when syntax
(Added Wren)
m (→‎{{header|Perl}}: removed deprecated given/when syntax)
Line 1,066:
 
=={{header|Perl}}==
<lang Perl>use 5.10.0strict; # for given/when
use warnings;
use feature 'say';
 
sub merge {
my ($x, $y) = @_;
my @out;
while (@$x and @$y) {
my $t = given ($x->[-1] <=> $y->[-1]) {;
if ($t when(== 1) { unshift @out, pop @$x }
elsif ($t == when(-1) { unshift @out, pop @$y }
else default { splice @out, 0, 0, pop(@$x), pop(@$y) }
}
@$x, @$y, }@out
return @$x, @$y, @out
}
 
sub strand {
my $x = shift;
my @out = shift @$x // return;
iffor (-@$x .. -1) {
push @out, splice @$x, $_, 1 if for (-@$x->[$_] ..>= $out[-1) {];
}
if ($x->[$_] >= $out[-1]) {
@out
push @out, splice @$x, $_, 1
}
}
}
return @out
}
 
sub strand_sort {
my @x = @_;
my(@out, @outstrand);
@out = merge \@out, \@strand while (my @strand = strand(\@x)) {;
}@out
@out = merge(\@out, \@strand)
}
@out
}
 
2,392

edits