Sorting algorithms/Bubble sort: Difference between revisions

Content added Content deleted
mNo edit summary
No edit summary
Line 51: Line 51:


{{array operation}}
{{array operation}}

==[[Perl]]==
'''Interpreter:''' perl 5.8.8

# Sorts an array in place and returns a copy
sub bubble_sort (@) {
my $len = @_ - 1;
for my $i (0..$len-1){
for my $j ($i+1..$len){
@_[$i,$j] = @_[$j,$i] if $_[$j] lt $_[$i];
}
}
return @_;
}

# Usage
@a = qw/G F C A B E D/;
bubble_sort(@a);

N.B. Of course, there's no need to implement bubble sort in perl as it has sort built-in.