Sorting algorithms/Selection sort: Difference between revisions

Content deleted Content added
Franck (talk | contribs)
Line 1,670: Line 1,670:


=={{header|Perl 6}}==
=={{header|Perl 6}}==
Solution 1:
<lang perl6>sub selection_sort ( @a is copy ) {
<lang perl6>sub selection_sort ( @a is copy ) {
for 0 ..^ @a.end -> $i {
for 0 ..^ @a.end -> $i {
Line 1,681: Line 1,682:
say 'input = ' ~ @data;
say 'input = ' ~ @data;
say 'output = ' ~ @data.&selection_sort;
say 'output = ' ~ @data.&selection_sort;
</lang>

{{out}}
<pre>input = 22 7 2 -5 8 4
output = -5 2 4 7 8 22
</pre>

Solution 2:
<lang perl6>sub selectionSort(@tmp) {
for ^@tmp -> $i {
my $min = $i;
$min = $_ if @tmp[$min] > @tmp[$_] for $i^..^@tmp;
@tmp[$i, $min] = @tmp[$min, $i] if $min != $i;
}
return @tmp;
}
</lang>
</lang>