Sorting algorithms/Insertion sort: Difference between revisions

→‎{{header|Perl 6}}: Added Perl 6 solution
No edit summary
(→‎{{header|Perl 6}}: Added Perl 6 solution)
Line 785:
Output:
-31 0 1 2 4 65 83 99 782
 
=={{header|Perl 6}}==
<lang perl6>sub insertion_sort ( @a is copy ) {
for 1 .. @a.end -> $i {
my $value = @a[$i];
my $j;
loop ( $j = $i-1; $j >= 0 and @a[$j] > $value; $j-- ) {
@a[$j+1] = @a[$j];
}
@a[$j+1] = $value;
}
return @a;
}
 
my @data = 22, 7, 2, -5, 8, 4;
say 'input = ' ~ @data;
say 'output = ' ~ @data.&insertion_sort;
</lang>
 
Output:<pre>input = 22 7 2 -5 8 4
output = -5 2 4 7 8 22
</pre>
 
=={{header|PHP}}==
<lang php>function insertionSort(&$arr){
256

edits