Skip to content

Instantly share code, notes, and snippets.

@puriney2
Created June 20, 2013 04:17
Show Gist options
  • Save puriney2/5820275 to your computer and use it in GitHub Desktop.
Save puriney2/5820275 to your computer and use it in GitHub Desktop.
PERL: Insertion Sort
# subroutine to implement insertion_sorting
#!/usr/bin/perl -w
sub insertion_sort {
my (@list) = @_;
foreach my $i (1 .. $#list){
my $j = $i;
my $tmp = $list[$i];
while ($j >0 && $tmp < $list[$j-1]){
$list[$j] = $list[$j-1];
$j --;
}
$list[$j]=$tmp;
}
return @list;
}
my @test = &insertion_sort(1,2,-1,3,0);
print "@test\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment