我的計算機科學教授今天開始教我們關於大O符號的問題,我很難理解它。下面是他給我們的例子:確定Java代碼的大O
什麼是下面的大O:
一個。 4n2 + 2是:O(n^2)?
b。 n2 + 14n + 6是:O(n^2)?
c。 5n3 + 10n2 - n - 8是:O(n^3)?
d。 3n2 + 2n是:O(n^2)?
據我所知,它與程序要多長時間運行基於輸入和多少將取決於輸入變化增加拿去做。我查了一個方法來確定上述問題的大O,並把我以爲是的。但是當談到確定Java代碼的大O時,我迷失了方向。有人能指出我對這些問題的正確方向嗎?
3. int count = 0;
for (int i = 1; i <= n; i++) {
i = n;
count += 1;
}
// end for
4. int total = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
for (int k = 1; three < j; j++) {
total += 1;
} //end for } // end for } // end for
5. int total = 0;
for (int one = 1; one <= n; one++) {
for (int two = 1; two < n; two++) {
for (int three = 1; three < 10; three++) {
total += 1;
} //end for } // end for } // end for
6. int total = 0;
for (int pass = 1; pass <= n; pass*=2) {
total += 1;
}
// end for
7. p = n;
while (n>1) {
n = n/2;
while (p>0) {
p = p - 1;
}
// end while } // end while
8. for (int i = 1; i <= n; i+=2) {
j=1;
while(j < n) {
j = j + 2;
x = x + 2;
} // end while } // end for
https://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/ –