8
votes

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://stackguides.com/questions/9556782/find-theta-notation-of-the-following-while-loop#=

2
You should link to the question you saw before.Douglas Zare

2 Answers

3
votes

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

2
votes

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