On Oct 10, 1:57 pm, [EMAIL PROTECTED] (Rob Dixon) wrote: > [EMAIL PROTECTED] wrote: > > > I'm having problems trying to figure out 'bubble sort'. I'm working on > > assignment for a class and I have to do the following: > > > Use the following code as a starting point for the next lab. > > > #!/usr/bin/perl > > > @array = (5,3,2,1,4); > > > ## include your code here ## > > > foreach $elem (@array){ > > print "$elem"; > > } > > > Use two loops and an if statement to sort the contents of the array so > > that the number go from lowest to highest. (This is actually a very > > inefficient way to sort an array.) > > > HINT: One loop should progress through the array comparing and > > swapping adjacent elements (if necessary). The second loop is used to > > make sure this happens multiple times based on the size of the array. > > > I can't for the life of me get this to work. I either hit an infinite > > loop or I get nothing. > > > Thoughts? > > Show us what you've written and explain how you mean it to work. We will > be able to explain where you have made mistakes. > > Rob
Here is what I had so far. I'm not sure exactly what I'm doing wrong. !/usr/bin/perl @array = (5,3,2,1,4); @tmp_array; $i = 0; while($i <= 5){ if ($array[$i] > $array[$i+1]); } while ($i > 2) { print "$tmp_array\n"; $i--; } foreach $elem (@array){ print "$elem"; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/