java - Order of growth of the code below -
what order of growth of worst case running time of following code fragment function of n? 
int sum = 0; (int = 0; i*i < n; i++) (int j = 0; j*j < 4*n; j++) (int k = 0; k < n*n; k++) sum++;
- outer loop runs until
i*i >= n
, means runs total of
sqrt(n)
times. - for each iteration of outer loop, inner loop runs until
j*j >= 4*n
, means runssqrt(4n) = 2sqrt(n)
times. - for each iteration of middle loop, inner loop runs until
k>=n*n
, meansn^2
iterations. - increasing sum done in constant time.
multiply above because (3) each iteration of (2), , (2) each iteration of (1), , sqrt(n)*2sqrt(n)*n^2 = 2n^3
, in o(n^3)
Comments
Post a Comment