寻找BigO - 一个while循环嵌套有两个for循环

时间:2015-02-27 08:47:46

标签: java arrays algorithm big-o

有人可以告诉我以下的BigO:

public void doFoo(int n) {
    int pass = 1;
    while (pass <= n) {
        for (int index = 0; index < n; index++) {
            for (int count = 1; count < 10; count++) {
                if (arr1[pass] == arr2[index]) {
                    arr1[pass]++;
                }
            }
        }
        pass = pass + 1;
    }
}

我得出结论O(n2),但我想澄清它是否正确。帮助赞赏。

1 个答案:

答案 0 :(得分:1)

答案是0(n^2) ..这是逻辑:

 while (pass <= n) {                     // executes n times
        for (int index = 0; index < n; index++) {      // executes n times
            for (int count = 1; count < 10; count++) { // always executes 9 times.. irrespective of "n". So. it doesn't matter.
                if (arr1[pass] == arr2[index]) {
                    arr1[pass]++;
                }
            }
        }