0

Possible Duplicate:
Big O, how do you calculate/approximate it?

I have a piece of code, like this:

// A
for (i = 0; i < 2*n; i += 2) {
    for (j=n; j > i; j--) {
        foo();
    }
}

// B
for(i=n; i>=0; i-=2) {
    for(j=i; j>n; j--) {
        foo();
    }
}

I know i have to "translate" the for cycles to a mathematic formula and check, which of those pieces (A or B) runs foo() more times.

For the mentioned case above is it

A: (n/2 + 1)*(n/2) B: 0

Is there any "cookbook" for that?

Community
  • 1
  • 1
Radek Simko
  • 15,886
  • 17
  • 69
  • 107

0 Answers0