开发者

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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜