使用数组运行语句的时间

时间:2014-07-13 03:22:40

标签: arrays time

所以我实际上很难计算或测量此代码的运行时间。它实际上由连续的陈述组成。

for(x=0; x<n; x++)//x=0 is 1, x<n is n+1, x++ is n, to sum up, it's 2n+2
    arr[x]=0; //How about an array? How do I compute a running time on an array?

for(x=0; x<n; x++) //x=0 is, x<n is (n-i)+1, y++ is (n-x)

    for(y=0;y<n;y++) //y=0 is zero, y<n is (n-y)+1, y++ is (n-j)
      arr[x]+=arr[y]+x+y; //and I am having a hard time on this line,
      //but I do know that += is 1, since there are two + sign,
      then that's two right? Then how about arr[x] and arr[y]?

我的问题在评论中,我们如何衡量数组的运行时间?像[x],[y]。

1 个答案:

答案 0 :(得分:0)

上述算法的渐近复杂度为O(n ^ 2)。主要贡献者是2 for for循环。在嵌套的for循环中,为每个x = [0,n]迭代n次。你也可以在计算复杂性时忽略常数,因为当n变得非常大时,常数的贡献可以忽略不计。