Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

This question already has an answer here:

Is the time complexity for this loop O(n) or O(log n)?

for(i=0;i<n/2;i++){}

I am thinking, the time is proportional with the input size.

share|improve this question

marked as duplicate by gnat, MichaelT, durron597, Snowman, BЈовић May 29 at 11:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Why do you think this might be O(log n)? –  Dan Pichelman May 28 at 13:04
    
Does that mean it's O(N)? –  Vlad Otrocol May 28 at 13:07
2  
Why do you think this might be O(n)? –  Blrfl May 28 at 13:07

4 Answers 4

O(n) since if n is extremely large (close to infinity), the fact that it is divided by two is negligent.

share|improve this answer

As written, it's O(n) i.e. if you double the size of n it'll take twice as long.

Any half decent compiler/JIT will, however, spot that it doesn't do anything and strip it out i.e. O(0). But I presume that's not what you're after.

share|improve this answer

It is O(n/2)=O(n), since complexity is calculated in order of magnitude and constants can be "ignored".

So, if c is a constant :

O(c*n)=O(n)
O(c*logn)=)(logn)

etc.

share|improve this answer

As written, it's O(1) because you do nothing in the loop. The compiler will optimize it away.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.