Re: is PHP crazy, or am I?
| From: | Christian Dechery | Date: | Sun, 09 Sep 2001 15:15:17 +0000 |
| Subject: | Re: is PHP crazy, or am I? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-67454@lists.php.net to get a copy of this message | ||
thanks for all your help... I got it after a bit of REAl thinking I got what that algorithm was meant to... it wasn't really the original bubble-sort... it's a kinda of pseudo-optimized 'one'... it goes like above:
function bubblesort(&$vetor,$tam)
{
$troca=0;
$i=0;
for($i=$tam; $i>0; $i--)
{
$troca=1;
for($j=0; $j<$tam ;$j++)
{
if($vetor[$j] > $vetor[$j+1])
{
$aux=$vetor[$j];
$vetor[$j]=$vetor[$j+1];
$vetor[$j+1]=$aux;
$troca=$j;
}
}
$tam=$troca;
}
}
well... enough about that poor sorting method... :)
At 16:45 14/9/2001 -0500, Richard Lynch wrote:
It's been far too long since I've done bubble sort versus Shell versus etc. If it's your girlfriend's homework, she should know, or know how to find out, better than either of us... -- 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 ----- 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: 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:p.s: meu novo email é cdechery@easynet.com.br ____________________________ . Christian Dechery (lemming) . http://www.tanamesa.com.br . Gaita-L Owner / Web Developerfunction bubblesort(&$vetor,$tam) {be$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 wouldO(n) (Actually it's exactly 1/2 n, but that's the same as O(n) forsufficientlylarge n.) Alas, that can't be correct (we wish) and is not the standard bubblesort...Try this for the body: for ($i = 0; $i < $tam -1; $i++){stillfor ($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) itwhat'sshows me it's not ordered... I printed it INSIDE the function...thething 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