特定情况下两个嵌套循环的时间复杂度

时间:2019-03-31 04:47:06

标签: algorithm data-structures time-complexity nested-loops

我有一个函数test(arr, x),其中arr是有限大小的数组,而x是有限的正数。我有两个嵌套的for循环,如下所示:

function test(arr, x) {
    for(i=0; i < arr.length; i++) { //executes arr.length times
        for(j=1; j <= x; j++) { // executes x times
            //code goes here
        }
    }
}

问题: 这种情况下的时间复杂度是多少?我对O(n)和O(n ^ 2)感到困惑。

任何帮助将不胜感激。预先感谢!

0 个答案:

没有答案
相关问题