Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: bubble sort in perl
by JayBonci (Curate) on Mar 24, 2002 at 10:47 UTC
    Perl has a built in search function, that you might want to take a look at; conveniently enough it's called sort.
    @raw_data = sort @raw_data;
    That will sort it in numerical order.
    @raw_data = sort {$b <=> $a} @raw_data
    will do so backwards. Full documentation is available at perldoc's sort documentation.

    Hope that is enough to get you started.     --jb
Re: bubble sort in perl
by RMGir (Prior) on Mar 24, 2002 at 13:21 UTC
    Keep in mind that bubble sort is one of the worst known sorting approaches. But given that, here's your sort (I gotta stop answering homework questions...)

    This isn't even an EFFICIENT bubble sort (hehe), since "isSorted" could be done as part of the bubbling loop, by bubbling until no swaps occur. But this version is probably easier to read.

    sub bubble { my $arrRef=shift; foreach my $i(1..@$arrRef-1) { @$arrRef[$i,$i-1]=@$arrRef[$i-1,$i] unless $arrRef->[$i-1]<=$arrRef->[$i]; } } sub isSorted { my $arrRef=shift; my $isSorted=0; foreach my $i(1..@$arrRef-1) { return 0 unless $arrRef->[$i-1]<=$arrRef->[$i]; } return 1; } open(IN,"<raw_data") or die "Can't open raw_data, error $!"; my @raw_data=map {chomp;$_} <IN>; print "Before sort: ",(join ",",@raw_data), "\n"; bubble(\@raw_data) while !isSorted(\@raw_data); print "After sort: ",(join ",",@raw_data), "\n";

    If this IS for homework, try to understand it before handing it in, k? Otherwise, your teacher's gonna ask you some tough questions, and you're going to be in deep trouble...

    Besides, if your teacher likes perl enough to teach it, he probably hangs out here with us anyhow :)
    --
    Mike

      Keep in mind that bubble sort is one of the worst known sorting approaches.

      ...for generalized data. for almost sorted data, it's the best algorithm. if you're maintaining a list of sorted data, adding a few items, and re-sorting, this may be the way to go.

      ~Particle ;Þ

        Well, not really.

        It's better than some, but you'd really want to do a selection or insertion sort to add your new elements, I think, depending on your data structure.

        The problem is that bubble sort only ever moves an element 1 spot. So if you add an element that would normally go into position 1 of a 1000-item list at the end of the pre-sorted list, you're looking at 1000 iterations over the whole list to "bump" it up into position 1. Ouch...
        --
        Mike

      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
      

Re: bubble sort in perl
by archen (Pilgrim) on Mar 25, 2002 at 04:41 UTC
    Er... not to imply that this is a homework assignment, but if you know what a bubble sort is, how come you can't do it on a simple array of integers? I mean there's nothing tricky about that; perl does a bubble sort like most any other language...