Re: Re: is PHP crazy, or am I?

From: Date: Fri, 14 Sep 2001 15:23:44 +0000
Subject: Re: Re: is PHP crazy, or am I?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-67379@lists.php.net to get a copy of this message
Did the assignment require this to be done in a non-standard way? If not, then you are over complicating a fairly simple algorithm. Just use two nested For loops as Richard suggested. If the assignment requires you to do it with a while loop (though for the life of me I can't imagine why it would) then you need make the following adjustments. 1) change your for loop to for($j=0; $j<$tam-$i-1; $j++) with your check being simply $tam-$i then the first time through you compare the last element in the array with n+1 which is an element out of the array's range. I am not sure how PHP handles this, but if it is anything like C then it is to be avoided at all costs (especially since there is a VERY good likelihood that you would proceed to write to n+1). This is all moot in your script, however, as you never actually get to the end of the array as we will see next. 2) Move $i++ OUT of your for loop. The way your script currently reads you are not even traversing your entire array once! Lets say you have a 4 element array... for($j=0; $j<$tam-$i; $j++) { <code> $i++ } First time: $j=0; $i=0; $tam-$i = 4 Run loop Second time: $j=1; $i=1 $tam-$i=3 Run Loop Third time: $j=2; $i=2; $tam-$i=2 Continue Past Loop So out of 4 elements, you looked at 2. Hope that helps Sheridan Saint-Michel Website Administrator FoxJet, an ITW Company www.foxjet.com ----- Original Message ----- From: "Christian Dechery" <cdechery@easynet.com.br> To: "Richard Lynch" <ceo@l-i-e.com> Cc: <php-general@lists.php.net> Sent: Friday, September 14, 2001 9:12 AM Subject: [PHP] Re: is PHP crazy, or am I? > 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 richard@zend.com address is an endangered species -- Use > >ceo@l-i-e.com > >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: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#67379) next »