6

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 outer loop has time complexity of O(logn), but since the iterations of the inner loop depends on the value of the outer loop, the complexity of this algorithm is not O(nlogn).

The book says it is O(n).

I really dont understand how it is O(n)...Can someone please explain it... I'll be really grateful if u could go into the details btw :D

A mathematical solution would help me understand better...

user2228135
  • 239
  • 6
  • 17
  • 2
    Nitpick: the loop *is* O(n log n), but *not* Theta(n log n). I don't know if all the universities make the distinction though... – Silly Freak Feb 06 '14 at 11:33

3 Answers3

6

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.
Abhishek Bansal
  • 12,589
  • 4
  • 31
  • 46
  • sorry, but i dont seem to understand... How does the O(logn) get elminated? Maybe some mathematical solution would help me to understand better.... – user2228135 Feb 06 '14 at 11:57
  • @user2228135 If you study the series, it is increasing geometrically. You can find the sum of that geometric series by the standard formula. – Abhishek Bansal Feb 06 '14 at 13:34
  • yes, geometric series. for example if n=4, then the result is `111` in binary which is one less than `1000`, which is 2n. (note: I would remove that sloppy explanation from your answer) – Karoly Horvath Feb 06 '14 at 14:29
  • The O(lg n) doesn't go away so much as it gets subsumed by the sum. The calculation isn't as simple as multiplying the complexities of the outer and inner loop; rather, you need to explicitly sum a series of O(lg n) terms, and that sum evaluates to O(n). – chepner Feb 06 '14 at 14:36
3

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] .

Tanmoy Banerjee
  • 1,433
  • 3
  • 22
  • 31
-1

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