0

Asked this at programmers Stack Exchange, was recommend to ask here :

What would be the big o for the algo:

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

As per my understanding 1st loop will loop upto n^2 times. 2nd loop will go around n^2 times.

Am I right or is it a O(n^6) representation.

Also is there a easy way to say when to use log. I have assumed that whenever the loop variable is multiplied the ln function is applied

eg: for (i=1; i<n*n i=i*2) Big o for the loop as per my understanding is O(ln n^2) ( Am I right again?)

Please clarify, thanks in advance

Raphael
  • 72,336
  • 29
  • 179
  • 389
new
  • 1
  • 1
    See our reference questions on this topic, http://cs.stackexchange.com/q/192/755 and http://cs.stackexchange.com/q/23593/755. They will show you how to turn this is into a summation, which you can then upper- and lower-bound using standard methods. – D.W. Oct 07 '14 at 18:24
  • 1
    Also, [tag:runtime-analysis+loops]. – Raphael Oct 07 '14 at 19:13
  • Well, I'm new here, so I haven't seen any answers to similar questions before. Assuming that there is a $\mathcal{O}(1)$ operation inside the inner loop, this inner loop has a running time of $\mathcal{O}(i^4)$. Thus, the outer loop will have a running time of $\sum_{i=0}^{n^2}i^4$, and that is at least $\mathcal{O}(n^8)$ (probably bigger). – Manolito Pérez Oct 08 '14 at 18:51

0 Answers0