[英]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.