I am studying Big-O notation right now and stumbled upon this little 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 cannot figure out how to do this. I think the complexity of the while loop is Θ (log (n)). The complexity of the for loop from what I thought would also be Θ (log (n)), because the number of iterations would be halved each time.
So, won't the complexity of all this be Θ (log (n) * log (n)), or am I doing something wrong?
Edit: the segment is in the best answer to this question: https://stackoverflow.com/a/4648772
algorithm big-o
Gerk
source share