簡體   English   中英

查找BigO-一個while循環與兩個for循環嵌套

[英]Finding BigO - One while-loop nested with two for-loops

有人可以告訴我以下內容的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),但我想澄清一下它是否正確。 幫助表示贊賞。

答案是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]++;
                }
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM