Created
June 20, 2013 04:17
-
-
Save puriney2/5820275 to your computer and use it in GitHub Desktop.
PERL: Insertion Sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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