Name: Anonymous 2011-04-28 15:06
I'm trying to do a bubble sort, selection sort, merge sort(not implemented yet) in perl but this randomly crashes no clue why any ideas...?
#!/bin/perl
&main();
sub main
{
my @array = (1,7,4,9,4,7,2,3,0,8);
print "Selection Sort\n\n";
&selectionSort(\@array,);
print "@array\n\n";
print "Bubble sorting\n\n"; #crashes here sort of clears the terminal screen and prints 8,9
my @secondarray =(7,8,9,3.2.10.12);
bubble_sort(@secondarray);
print "@secondarray\n\n";
sleep(10);
} # main
sub selectionSort
{
my $aref = shift(@_);
for (my $x=0; $x<@$aref-1; $x++)
{
my $smallestIndex = $x;
for (my $y=$x+1; $y<@$aref; $y++)
{
if ( $aref->[$y] < $aref->[$smallestIndex] )
{
$smallestIndex = $y;
}
} # for $y
&swap( \$aref->[$x], \$aref->[$smallestIndex] );
} # for $x
} # selectionSort
sub swap
{
my ($x, $y) = @_;
my $tmp = $$x;
$$x = $$y;
$$y = $tmp;
} # swap
sub bubble_sort {
for my $i (0 .. $#_){
for my $j ($i + 1 .. $#_){
$_[$j] < $_[$i] and @_[$i, $j] = @_[$j, $i];
}
}
}