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

2001-09-15 Thread Christian Dechery

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; $i0; $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 [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
- Original Message -
From: Christian Dechery [EMAIL PROTECTED]
To: Richard Lynch [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
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:
  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


p.s: meu novo email é [EMAIL PROTECTED]

. Christian Dechery (lemming)
. http://www.tanamesa.com.br
. Gaita-L Owner / Web Developer


-- 
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]




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

2001-09-14 Thread Christian Dechery

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]




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

2001-09-14 Thread Sheridan Saint-Michel

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 [EMAIL PROTECTED]
To: Richard Lynch [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
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 [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]


-- 
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]




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

2001-09-13 Thread Richard Lynch

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]