ersin.fr
msgbartop
just my personal notebook…
msgbarbottom


29 Dec 16 Bubble sorting with Perl

Advertisements
#!/usr/bin/perl 
use strict;
use Data::Dumper;

sub swap{
     @_[ 0, 1 ] = @_[ 1, 0 ];
}

sub bsort{
     my (@a) = @_;
     for my $j (0 .. $#a){
           for my $i (0 .. $#a - 1 - $j ){
                swap $a[$i], $a[ $i + 1 ] if $a[$i] > $a[ $i + 1 ];
           }
     }
 \@a;
 }



#Testing 
my $a=1;
my $b=3;
my @c=(3,2,1,4);
print "First:".Dumper(@c);
print "\nLatest:".Dumper(bsort(@c));

Tags: