Sorting algorithms/Bubble sort: Difference between revisions

Undo revision 125909 by Crownf (talk) screwed up section
(Undo revision 125909 by Crownf (talk) screwed up section)
Line 2,028:
</lang>
 
=={{header|Perl}}==
01 #!/usr/bin/perl
<lang perl># Sorts an array in place
02
03 sub bubblesortbubble_sort {
04 for my $arrayi =(0 shift;.. $#_){
12 for (my $j =($i + 1; $j <=.. $i; $j++ #_) {
05
06 my $i; $_[$j] < #$_[$i] Theand initial@_[$i, index$j] for= the@_[$j, bubbling scan.$i];
19 }
07 my $j; # The running index for the bubbling scan.
20 }
08 my $ncomp = 0; # The number of comparisons.
}</lang>
09 my $nswap = 0; # The number of swaps.
 
10
Usage:
11 for ( $i = $#$array; $i; $i-- ) {
 
12 for ( $j = 1; $j <= $i; $j++ ) {
13 <lang perl>my @a = (39, 25, 30, 28, 36, 72, 98, 25, $ncomp++43, 38);
bubble_sort(@a);</lang>
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}}==
Anonymous user