Perl Bubble-Sort example

This is just an example sort, for real use you should take the intern sort from Perl.

#!/usr/bin/perl
use strict;
#example array
my @arr = (4,7,2,77,8,4,9,3,35,7,678,1,9);
#test
print join(",",@arr)."n";
@arr = bubblesort(@arr);
print join(",",@arr)."n";
#sort sub
sub bubblesort {
    my @array = @_;
    for(my $i=0,my $c=1;$i <= $#array;++$i){
        if($array[$i] < $array[$i+1]){
            $c = $array[$i];
            $array[$i] = $array[$i+1];
            $array[$i+1] = $c;
            $i = -1;
            }
    }
    return(@array);
}

The Result looks like this:

4,7,2,77,8,4,9,3,35,7,678,1,9 
678,77,35,9,9,8,7,7,4,4,3,2,1

Leave a Reply

Your email address will not be published.

twelve + twenty =