in reply to Re: bubble sort in perl
in thread bubble sort in perl

Okay, here's a somewhat more efficient numeric bubble sort. (taking a list instead of arrayref, just like perl's sort.)

sub bubble { my @array = @_; for my $i (0..$#array) { for my $j (0..$i) { @array[$i, $j] = @array[$j, $i] if $array[$i] < $array[$j]; } } return @array; }


But:

Thou shalt not sort with bubbles!

Perl's sort is a lot easier, and a hell of a lot more efficient. If this is a homework assignment, and it probably is, don't just use our answers, but try to figure out WHY it works, and then tell your teacher real coders use sort instead.

If it's part of a job application test, make sure you're not dealing with talexb and go ahead with a more efficient sort.

U28geW91IGNhbiBhbGwgcm90MTMgY
W5kIHBhY2soKS4gQnV0IGRvIHlvdS
ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
geW91IHNlZSBpdD8gIC0tIEp1ZXJk