for(int i=0;i<n;i++)
{
// Some code
}
We generally say that this loop runs for n+1 times so n+1 steps for this and there is one step is of initialisation i=0.
This I have read in most of the text books.
My question is this that everytime the loop runs there is one more step of incrementing i to i+1 that is i=i+1, this is also one of the step which should be counted in calculating the time complexity.As I am a newbie to algorithm analysis help me with this problem.
No, we say it runs for n iterations. That’s the point in combining a starting index of
0with the boundary-check written as< n. It will exit the loop once the counter reachesn, after having takenniterations (one for 0, one for 1, one for 2, … one for n – 1, then exit).The work done to increment the counter, be it
i++,i += 1ori = compute_the_next_index(i), does not count as a “step”. The steps are the iterations, i.e. the executions of the loop’s body.