Jump to content

Sorting algorithms/Insertion sort: Difference between revisions

→‎{{header|Perl}}: an insertion sort should become O(n) when already sorted so the splice method from before wouldnt be good.
(→‎{{header|C}}: Keep indexing the same as the shell sort)
(→‎{{header|Perl}}: an insertion sort should become O(n) when already sorted so the splice method from before wouldnt be good.)
Line 839:
<lang perl>
sub insertion_sort {
my (@a, $i, $j, $k) = @_;
for my ($i (0= ..1; $#i < @a; $i++) {
for my $jk (0 ..= $a[$i - 1) {];
for ($j = $i if- 1; ($a[j >= 0 && $i]k < $a[$j]; $j--) {
$a[$j + 1] my= $k = splice @a, [$i, 1j];
splice @a, $j, 0, $k;
last;
}
}
$a[$j + 1] = last$k;
}
@a;
}
 
 
my @a = (4, 65, 2, -31, 0, 99, 83, 782, 1);
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.