Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: Big O - Algorithm Analysis

1. ## Big O - Algorithm Analysis

How do I analyze a for loop that looks like

for (j = 0; j < 2*n; j += 2)

I know that if it was just j++ it would loop 2n times but the += is throwing me off. This loop is nesting in a normal loop of

for(i=0; i < n; i++)

I know this will make it n^2 but I don't know how the j += 2 affects the time.

Thanks for the help,
PeskyToaster

2. ## Re: Big O - Algorithm Analysis

`j += 2`
just adds 2 to j, it is the same as
` j = j + 2`
, and because j is incrementing twice every loop, it is going to reach it's end twice as fast, so it is going to be looping
`1/2 * 2n`
or
`n`
times.

3. ## The Following User Says Thank You to Tjstretch For This Useful Post:

PeskyToaster (March 15th, 2012)

4. ## Re: Big O - Algorithm Analysis

That loop is the same as for (int index = 0; index < N; index++) in terms on time complexity.

5. ## Re: Big O - Algorithm Analysis

And, does the factor of 2x or 0.5x or whatever was worrying you, really matter?

6. ## Re: Big O - Algorithm Analysis

I have another problem. First here's the code snippet. It's just general code for analysis

```
for (j=0; j<2n; j++) {
i = 1;
Generic Statement;
while (j<n) {
Generic statement;
Generic statement;
i = i *2;
}
}```

How does the i = 2n and the j < 2n affect the Big O and execution time. The execution time is just the equation with each statement being one time unit.