寻找 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]++;
}
}
}
谁能告诉我以下的 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]++;
}
}
}