Created
January 9, 2014 16:47
-
-
Save mussatto/8337515 to your computer and use it in GitHub Desktop.
selection sort in perl
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
use strict; | |
use warnings; | |
my @random=(); | |
for(my $i=0; $i<=100;$i++){ | |
$random[$i] = int( rand(1000)); | |
} | |
sub selection_sort{ | |
my @values = @{$_[0]}; | |
my $pivot; | |
my $values_size = @values; | |
my $change_position; | |
for(my $i=0; $i< $values_size; $i++){ | |
$pivot = @values[$i]; | |
$change_position = $i; | |
for(my $j=$i; $j< $values_size; $j++){ | |
if($pivot > @values[$j]){ | |
$pivot = @values[$j]; | |
$change_position = $j; | |
} | |
} | |
my $aux = $values[$i]; | |
$values[$i]=$pivot; | |
$values[$change_position] = $aux; | |
} | |
return @values; | |
} | |
my @sorted_array = selection_sort(\@random); | |
print "random numbers:", "\n"; | |
for my $number (@random){ | |
print $number,","; | |
} | |
print "-----------------------------","\n"; | |
print "sorted_array:", "\n"; | |
for my $number (@sorted_array){ | |
print $number,","; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment