Big O Notation.

Would anybody know what this means? Having trouble understanding it.

Characterize the following algorithm in terms of Big-O notation. For full credit, explain in 1-3 sentences your reasoning.

1
2
3
4
5
6
7
8
9
     for  (int  i  =  1;  i  <=  2  *  n;  i++) 

         for  (int  j  =  1;  j  <=  n;  j++) 

             cout  <<  2  *  i  +  j; 

     cout  <<  endl;   

The most I can think of is that the second for loop will run N amount of times depending on the first loop.
When you see questions like this, focus on the inner most loop first then look at the the next up till the outermost. Most of the time, the number of times that inner loop runs will tell you the big O complexity of the entire code snippet.
What do you mean by that?
Hi,

If you had:

1
2
3
4
5
for  (int  i  =  0;  i  <  a;  i++) {
             for  (int  j  =  0;  j  < b;  j++) {
                //some code here
             }
}


Then those loops would run a total of a * b times , right?

So apply that thinking to your code :+)
Topic archived. No new replies allowed.