Time complexity of this code sample
i=n;
while (i>=1) {
--x=x+1;
--i=i/2;
}
What is the running time of this code?
A O(N^2)
B O(N^3)
开发者_Go百科C O(N^4)
D O (LOG N)
E O(2^N)
I believe it is the option D
This is for revision. Not homework
This will never terminate as the while condition is
i>=i
However, assuming you wanted to type
i>=1
The answer will be log(n).
Your belief would be correct if you change the while condition to i>=1
As it stands the complexity is O(INFINITY)
精彩评论