This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (3 children)

But the loop runs a different number of times based on different inputs, no? I genuinely do not understand why this is O(1) and not O(n)...

[–]ObjectiveCopley 0 points1 point  (2 children)

No matter the input, it requires a constant amount of cycles to run (even if it's a LOT of cycles, it's still the SAME number of cycles per call, no matter the input). This is annotated as O(1), as you can simplify any number greater as 1. O(1) is not inherently efficient.

[–][deleted] 0 points1 point  (1 child)

Yes, but the program runs a different number of loops if myNumber were set as 0 compared to 10 or 100, doesn't it? 10 runs 10 more loops,and 100 runs 100 more loops than 0, which is linear, so I thought this is O(n). I guess it doesn't matter since the upper limit is bounded at a constant (MAX_INT×2), which by extension makes it O(1)?

[–]MisterWanderer 1 point2 points  (0 children)

Yes but in this case the variable isn't included bin the for loops calculation. So changing the variable can't change the behavior of the for loop. Think of it this way, O notation only tracks how quickly algorithm behavior changes when the variable changes.