Time complexity for dependant nested for loop?

前端 未结 3 1976
庸人自扰
庸人自扰 2021-01-13 04:43

Can you explain me how to find time complexity for this?

sum=0;
for(k=1;k<=n;k*=2)
  for(j=1;j<=k;j++)
     sum++;

So, i know the out

相关标签:
3条回答
  • 2021-01-13 05:23

    I believe you should proceed like the following to formally obtain your algorithm's order of growth complexity, using Mathematics (Sigma Notation):

    enter image description here

    0 讨论(0)
  • 2021-01-13 05:24

    Just see how many times the inner loop runs:

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

    Note that if n = 32, then this sum = 31 + 32. ~ 2n.
    This is because the sum of all the terms except the last term is almost equal to the last term.

    Hence the overall complexity = O(n).

    EDIT:

    The geometric series sum (http://www.mathsisfun.com/algebra/sequences-sums-geometric.html) is of the order of:

    (2^(logn) - 1)/(2-1) = n-1.
    
    0 讨论(0)
  • 2021-01-13 05:40

    The outer loop executed log(Base2)n times.so it is O(log(Base2)n).

    the inner loop executed k times for each iteration of the outer loop.now in each iteration of the outer loop, k gets incremented to k*2.

    so total number of inner loop iterations=1+2+4+8+...+2^(log(Base2)n)
    =2^0+2^1+2^2+...+2^log(Base2)n (geometric series)
    =2^((log(base2)n+1)-1/(2-1)
    =2n-1.
    =O(n)
    so the inner loop is O(n). So total time complexity=O(n), as O(n+log(base2)n)=O(n).

    UPDATE:It is also O(nlogn) because nlogn>>n for large value of n , but it is not asymptotically tight. you can say it is o(nlogn)[Small o] .

    0 讨论(0)
提交回复
热议问题