views:

86

answers:

2

Just a confusion.... A portion of a C++ sample code is as follows


I just re-edit the whole post. Sorry for any confusion

int i, j;   
i = 0;    // c1
j = 0;    // c2
while (i < n)   // (n+1)c3
{
  i++;   // n c4
   while ( j < i)  // (2+3+....+n+(n+1)c5
   {
     j++;   // (1+2+...+n)c6
   }
  j = 0;  // c7
}

Clearly, c1, c2, and c7 are constant. We are not interested in those, and they don't really matter in determing the running time.

What I don't understand is c5 and c6.

Why is it 2+3+4...+n+(n+1) for c5? Why does it start with 2, instead of 1+2+3...+(n+1)??? Note that we can rewrite C5 -> (n*(n-1)/2) + n

For c6, this cominbation can be rewritten as n*(n-1)/2

Initially I thought C6 is n, because C6 depends on two conditions, the first while and second while loop. But since j will always go back to 0, so we are really depending on the first while loop. Because n < n is false, then the j++ wil run maxiumly n-th time.

n = 3

0 < 3, 1, 0 < 1, 1, 0

1 < 3, 2, 0 < 1, 1, 0

2 < 3, 3, 0 < 1, 1, 0

3 < 3 fail.

Can someone please explicitly explain how C5 and C6 are deteremined? I am sorry if this problem sound dumb to the experts Thank you!

+3  A: 

I don't entirely understand your question but it seems to me that you need to move the initialization of j inside the loop:

while (i < n)   
{
    j = 0;    // <--- here
    i++;   
    // etc...
}
Mark Byers
thanks. I forgot the j=0 at the end.
JohnWong
+4  A: 

Here, you have a running time of 2n. Everytime i is incremented, j is one smaller, so the inner loop is executed exactly once.

i=0, j=0 // init
i=1, j=0 // outer loop
i=1, j=1 // inner loop
i=2, j=1 // outer loop
i=2, j=2 // inner loop

More typically, you'd reset j to 0 in the outer loop. In that case, you'd have a runtime of n*(n-1)/2

Nicolas78
right i forgot the j = 0 at the end.
JohnWong
So this is what I had according to the right note (with j = 0 outside). n = 30 < 3, 1, 0 < 1, 1, 0 ( respectively)1<3, 2, 0 <1, 1, 02<3, 3, 0 < 1, 1, 03< 3, end.What I am confused with is the 2+3+....(n+1) running time for the 2nd while-loop.
JohnWong
the thing is that the inner loop is executed to bring j to exactly i. in fact, that might even mess up my first guess. there's a bit of a difference to a for loop that would not enter the inner loop as soon as the condition is violated. but here, since the condition is violated only in the inner loop, you get the additional count!
Nicolas78
i can see that the j++ goes linearly. the amount of running is 1.....n
JohnWong
thank you for the reply. sorry nicolas. i re-edited the code to remove all confusions. i am not sure what you mean by the addiitonal count. why did it start with 2, instead of 1, to begin with?
JohnWong
I don't even really understand what "it starts with 2" means...?
Nicolas78
Thank you. Hmmm for c5, we have the running time 2+3+4+...n+(n+1), which can be rewritten as (n*(n-1)/2) + n. Why did it starts with 2, instead of 1, like C6 has. I just looked at every comment, and every line, and I realize what I said about c6 is also wrong. I thought it would be n, instead of 1+2+3+4...+n which can be rewritten in the form n*(n-1)/2.
JohnWong
I just re-edit the whole post. I hope this time I am not causing any more confusion for the reader :)
JohnWong