开发者

Time complexity of this code sample

开发者 https://www.devze.com 2023-03-08 17:58 出处:网络
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百科
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

精彩评论

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