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.