algorithm - Big-O analysis for a loop -
i've got analyze loop, among others, , determine running time using big-o notation.
for ( int = 0; < n; += 4 ) ( int j = 0; j < n; j++ ) ( int k = 1; k < j*j; k *= 2 )`
here's have far:
for ( int = 0; < n; += 4 ) = n
for ( int j = 0; j < n; j++ ) = n
for ( int k = 1; k < j*j; k *= 2 ) = log^2 n
now problem i'm coming final running time of loop. best guess o(n^2), uncertain if correct. can help?
edit: sorry oh -> o thing. textbook uses "big-oh"
first note outer loop independent remaining 2 - adds (n/4)*
multiplier. consider later.
now let's consider complexity of
for ( int j = 0; j < n; j++ ) ( int k = 1; k < j*j; k *= 2 )
we have following sum:
0 + log2(1) + log2(2 * 2) + ... + log2(n*n)
it note log2(n^2) = 2 * log2(n). re-factor sum to:
2 * (0 + log2(1) + log2(2) + ... + log2(n))
it not easy analyze sum take @ this post. using sterling's approximation 1 can belongs o(n*log(n))
. overall complexity o((n/4)*2*n*log(n))= o(n^2*log(n))
Comments
Post a Comment