Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time complexity of the following algorithm?

I'm learning Big-O notation right now and stumbled across this small algorithm in another thread:

i = n
while (i >= 1)
{
    for j = 1 to i  // NOTE: i instead of n here!
    {
      x = x + 1
    }
    i = i/2
}

According to the author of the post, the complexity is Θ(n), but I can't figure out how. I think the while loop's complexity is Θ(log(n)). The for loop's complexity from what I was thinking would also be Θ(log(n)) because the number of iterations would be halved each time.

So, wouldn't the complexity of the whole thing be Θ(log(n) * log(n)), or am I doing something wrong?

Edit: the segment is in the best answer of this question: https://stackoverflow.com/questions/9556782/find-theta-notation-of-the-following-while-loop#=

like image 803
Gerk Avatar asked May 07 '15 19:05

Gerk


2 Answers

Imagine for simplicity that n = 2^k. How many times x gets incremented? It easily follows this is Geometric series

2^k + 2^(k - 1) + 2^(k - 2) + ... + 1 = 2^(k + 1) - 1 = 2 * n - 1

So this part is Θ(n). Also i get's halved k = log n times and it has no asymptotic effect to Θ(n).

like image 194
JuniorCompressor Avatar answered Oct 14 '22 19:10

JuniorCompressor


The value of i for each iteration of the while loop, which is also how many iterations the for loop has, are n, n/2, n/4, ..., and the overall complexity is the sum of those. That puts it at roughly 2n, which gets you your Theta(n).

like image 38
Scott Hunter Avatar answered Oct 14 '22 18:10

Scott Hunter