Double Cycle Complexity

I am trying to figure out the complexity of the for loop using Big O notation. I have done this before in other classes, but this one is more strict than others because it is on the algorithm itself. The code is as follows:

for(cnt = 0, i=1; i<=n; i++) //for any size n
{
    for(j = 1; j <= i; j++)
    {
       cnt++;
    }
}

and

for(cnt = 0, i=1; i<=n; i*=2) //for any size n
{
    for(j = 1; j <= i; j++)
    {
       cnt++;
    }
}

I came that the first loop has O (n) complexity because it goes through n times. As for the second cycle, I got a little lost. I believe that it goes through the cycle I times for each tested n. I (incorrectly) assumed that this means that the loop is O (n * i) for each of its calculations. Is there something that I am missing in my assumption. I know cnt ++ is constant time.

Thanks for the help in the analysis. Each cycle is in its own space, they are not together.

+5
3

n . i , : .. n n - .

1+2+3+4+5+...+n = (n*(n-1))/2 --> O(n^2)

: i , Log2(n) . , n 2,

1+2+4+8+16+...+n

2^Log2(n)-1 = n-1 O(n).

n, , (2^(Log2(n)+1))-1, O(n):

1      -> 1
2..3   -> 3
4..7   -> 7
8..15  -> 15
16..31 -> 31
32..63 -> 63

..

+7

, , , , :

cnt n * (n + 1)/2 , O (n ^ 2). O (n/2) , O (n).

0

: O (N ^ 2) Big-O , ? , , , , n.

, , O (n log n), , . . O (log n), - O (n), , O (n log n).

0

All Articles