Annita Zirki
Annita Zirki

Reputation: 147

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)

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

Upvotes: 0

Views: 158

Answers (2)

Apoorv
Apoorv

Reputation: 1111

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

Upvotes: 2

d-live
d-live

Reputation: 8036

Your belief would be correct if you change the while condition to i>=1 As it stands the complexity is O(INFINITY)

Upvotes: 0

Related Questions