I must have deleted the line by accident...

$trocou=true; is right below $vetor[$j+1]=$aux;

would that make sense?

I'm not worried about the optimization here... I want to get time results 
for bubblesort... and then I'm going to heap, quick, and others... It's 
some college homework for my girlfriend...


At 00:07 14/09/01 -0500, you wrote:
>function bubblesort(&$vetor,$tam)
>{
>     $trocou=true;
>     $i=$j=0;
>
>     while($trocou)
>     {
>         $trocou=false;
>
># You set $trocou to false here, and never reset it to true
># This loop will execute exactly once.
>
>         for($j=0; $j<$tam-$i; $j++)
>         {
>             if($vetor[$j] > $vetor[$j+1])
>             {
>                 $aux=$vetor[$j];
>                 $vetor[$j]=$vetor[$j+1];
>                 $vetor[$j+1]=$aux;
>             }
>             $i++;
>         }
>     }
>}
>
> > it's right right?
>
>No.
>
>As near as I can figure, you are incrementing $j and decrementing $i on
>every iteration.
>That would mean that even if you fixed $trocou your sort algorithm would be
>O(n)
>(Actually it's exactly 1/2 n, but that's the same as O(n) for sufficiently
>large n.)
>
>Alas, that can't be correct (we wish) and is not the standard bubble sort...
>
>Try this for the body:
>for ($i = 0; $i < $tam -1; $i++){
>     for ($j = $i + 1; $j < $tam -1; $j++){
>         if ($vetor[$i] < $vetor[$j]){
>             $aux = $vetor[$i];
>             $vetor[$i] = $vetor[$j];
>             $vetor[$j] = $aux;
>         }
>     }
>}
>
>Forget the trocou bit.
>
> > so why when I print the array (with a for(;;) or with print_r) it still
> > shows me it's not ordered... I printed it INSIDE the function... what's
>the
> > thing here? I'm I nuts?
>
>--
>WARNING [EMAIL PROTECTED] address is an endangered species -- Use
>[EMAIL PROTECTED]
>Wanna help me out?  Like Music?  Buy a CD: http://l-i-e.com/artists.htm
>Volunteer a little time: http://chatmusic.com/volunteer.htm


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to