Is the initialization part included when calculating the loop-type time?

Asked 2 years ago, Updated 2 years ago, 85 views

I'm a soldier who takes a data rescue class by himself.

Honestly, I wonder why I'm asking this question myself, I'm asking you a question to get an answer.

Suppose the for statement is as follows.

for( i = 1; i < n ; i ++ ) {
    i = i + 1;
}

In this case, I thought:

Obviously, Big O is O(n) here, but

Here, it is confusing whether the number of times is 3 or 2.

Let's see

I'm curious.

I'm sure you'll all say this

The main is time complexity, not repetition. In the end, it is only a process of calculating the efficiency of the function by obtaining the big oh.

Yeah. He's just curious about this. If you don't understand why this is happening, you can't fall for it

algorithm big-o loops

2022-09-20 17:28

1 Answers

I'm sorry, but if the code you wrote is n=3, it doesn't go around the loop twice, it doesn't go around 3 times, and it only goes around once.

by the way

Does the loop-type time calculation include the initialization part?

If I only answer the question, I don't think so.
If you're running a very heavy operation, you know, the initialization part that runs once really doesn't affect the loop speed. (And complexity is a concept that's far from hard for a machine to compute. I'm sure you understand this part.)


2022-09-20 17:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.