-3

I'm confused about the complexity of the following (the operation performed inside the inner loop is in constant time):

Pseudocode:

for i = 1 to n
   for j = i to n
      for k = i to j
         x := x + 1;
      end for
   end for
end for;

Code:

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

O(n^3) ?

  • `question-mauvaise.c:4:3: unexpected token '???', did you mean '?'?` –  Mar 14 '13 at 21:44
  • Can you please format your code properly? And ideally translate it? – djechlin Mar 14 '13 at 21:45
  • And honestly, you need to read http://whathaveyoutried.com. I can think of a dozen ideas here you might have to get started and don't think there is much honor in helping you avoid doing that. – djechlin Mar 14 '13 at 21:47

2 Answers2

1

O(n^3) ???

Yes, even if you don't bother translating your homework from French.

0

It's the product of O(outer loop in outer loop control) * O(inner loop in inner loop control).

djechlin
  • 59,258
  • 35
  • 162
  • 290