开发者

Big O Notation for with n^3 Nested For loops

Considering the following code:

for ( int j = 0; j < 2n; j++)
{
    for ( int k = 0; k < n^3; k += 3)
        sum++;
}
开发者_StackOverflow

Is the complexity O(n^2)? Does the n^3 in the for loop affect the Notation for LARGE N?


O(N^4)

sum++ is called 2*n*(n^3)/3 times.


if you only consider the inner loop, it gets executed N^3 times

the outer loop makes the inner one execute N times, so total complexity = N * N^3 = N^4


The outer loop has O(2n) operations.
The inner loop has O(n^3) operations.
Together, the program has O(n)*O(n^3) = O(N^4).


The exact number of iterations and the order of growth complexity can be inferred formally:

Big O Notation for with n^3 Nested For loops

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜