java - Finding the computational complexity of nested loops -
i'm not sure if i'm doing these problems correctly need tell me if i'm wrong.
( = 0 ; < n ; ++ )
this n-0 = n assignment , it's o(g(n)) right?
okay know, if want number of assignments , o(g(n)) in these questions:
sum = 0; ( = 1 ; < n * n ; j ++) { ( j = 1 ; j <= n; j ++ ) { sum += j; } }
what did was, sum=0 1 assignment , outer loop n^2 - 1 assignments , inner loop n-1 assignments , sum 1 assignment
therefore, number of assignments 2+(n^3+1) gives o(g(n^3))
in nested loop :
sum = 0; ( = 1 ; <= n; ++ ) { ( j = 1 ; j <= 100 ; j++) { ( k = 1 ; k <= n ; k ++ ) { sum += k; } } }
what did , sum =0 1 assignment first loop 1-n assignments second loop 99 last loop 1-n , sum = 2
so got 3+(1+n^2) assignments give me o(g(n^2))
is there wrong did?
your calculations correct. thing might - it's important whether it's o(g(n^2)) or o(g(99*n^2)). multiplier becomes not-so-important on large-scaled n values, @ small scales - constant factor of 99x times iterations (when not optimized compiler) might important.
Comments
Post a Comment