找出以下'for'循环的计算复杂性

时间:2015-01-03 15:02:09

标签: big-o complexity-theory time-complexity

以下for循环的复杂性是什么?

n = 0;
for (i = 1; i <= N; i++)
{    
    s(i) = sum of the first i coordinates of a
           fixed vector (of dimension N);

    % suppose r(i) (for all i) is already computed
    if (r(i) s(i) > r(n) s(n))
        n = i;  
}

1 个答案:

答案 0 :(得分:1)

你的循环最坏情况的复杂性是o(N²)。

循环执行N次。每次迭代都有另一个依赖N,因为:

s(i) = sum of the first i coordinates of a fixed vector (of dimension N);

我在这里看不到N的任何依赖,所以在http://en.wikipedia.org/wiki/Big_O_notation它只是不变:

% suppose r(i) (for all i) is already computed
if (r(i) s(i) > r(n) s(n))
    n = i;