Anonymous user
Sorting algorithms/Bubble sort: Difference between revisions
→{{header|Perl}}
(Undo revision 124529 by 38.116.192.95 (talk) Vandalism) |
|||
Line 2,028:
</lang>
01 #!/usr/bin/perl
02
03 sub
04
05
for my $j ($i + 1 .. $#_){▼
06 my $i;
07 my $j; # The running index for the bubbling scan.
}▼
08 my $ncomp = 0; # The number of comparisons.
}▼
09 my $nswap = 0; # The number of swaps.
10
11 for ( $i = $#$array; $i; $i-- ) {
14 # Swap if needed.
15 if ( $array->[ $j - 1 ] gt $array->[ $j ] ) {
16 @$array[ $j, $j - 1 ] = @$array[ $j - 1, $j ];
17 $nswap++;
18 }
▲19 }
▲20 }
21 print "bubblesort: ", scalar @$array,
22 " elements, $ncomp comparisons, $nswap swaps\n";
23 }
24
25 @array = qw(question auteurity and eat the rich goodness of duncan hines);
26
27 bubblesort \@array;
28
29 print "@array\n";
=={{header|Perl 6}}==
|