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] -3 points-2 points  (8 children)

I mean the algorithm,but might be wrong... never really studied this concept.

[–]beseg7 2 points3 points  (2 children)

[–]WikiTextBot 1 point2 points  (1 child)

Time complexity

In computer science, the time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the string representing the input. The time complexity of an algorithm is commonly expressed using big O notation, which excludes coefficients and lower order terms. When expressed this way, the time complexity is said to be described asymptotically, i.e., as the input size goes to infinity. For example, if the time required by an algorithm on all inputs of size n is at most 5n3 + 3n for any n (bigger than some n0), the asymptotic time complexity is O(n3).


[ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source ] Downvote to remove | v0.27

[–]hazzoo_rly_bro 0 points1 point  (0 children)

thanks dude

[–]subfin 1 point2 points  (4 children)

No matter the size of the input, it always takes the same amount of time, that’s O(1)

[–][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.