Big O Notation.

Mar 6, 2015 at 3:12am
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;   

Mar 6, 2015 at 3:22am
The most I can think of is that the second for loop will run N amount of times depending on the first loop.
Mar 6, 2015 at 3:58am
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.
Mar 9, 2015 at 5:05am
What do you mean by that?
Mar 9, 2015 at 6:26am
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.