"John W. Krahn" wrote:
> That looks like a direct translation of algorithm 5.2.2B from TAoCP Vol.
> 3 however the usual implementation is more like Sedgewick's example:
>
> sub bubble {
> my $a = shift;
>
> for ( my $i = @$a; $i >= 1; $i-- ) {
> for ( my $j = 2; $j <= $i; $j++ ) {
> if ( $a->[ $j - 1 ] > $a->[ $j ] ) {
> $a->[ $j - 1, $j ] = $a->[ $j, $j - 1 ];
> }
> }
> }
> }
>
Oh my dear, that's really unperlish :-)
What do you think of:
sub bubble {
my $a = shift;
my @a = @$a;
foreach my $i (reverse (1 .. @a)) {
foreach my $j (2 .. $i) {
($a[$j-1], $a[$j]) = ($a[$j], $a[$j-1]) if $a[$j-1] > $a[$j];
}
}
return $a;
}
Note, that I changed the assignment, too, so now it should work :-)
Greetings,
Andrea
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]