请帮助,我不确定我在以下示例中对Big-O的推理是否正确。有人可以在这里思考纠正任何错误。不确定' i'递增变量影响用户输入的数据' n'在这些情况下。而且通常只是不确定我是否正确理解大O. 谢谢
例1:
for (i = 1; i < (n*n+3*n+17) / 4 ; i = i+1) // 1/4*(n^2+3n+17) since 1/4n^2 dominates it's O(n^2), curious how the i = i + 1 effects things, please help
System.out.println("Sunshine"); // negligible time O(1)
// Total = O(n^2) + O(1) = O(n^2)
例2:
for (i = 0; i < n; i++) // O(n) here
if ( i % 2 == 0) // not sure here
for (j = 0; j < n; j++) // not sure
System.out.print("Bacon"); // negligible time O(1)
else
for (j = 0; j < n * n; j++) // not sure
System.out.println("Ocean"); // negligible time O(1)
// Total = ?
示例3:
for (i = 1; i <= 10000 * n: i = i*2) // 1000*n so O(n) how does i = i * 2 effect things, please help
x = x + 1; // negligible time O(1)
// Total = O(n) + O(1) = O(n)
范例4:
for ( i = 1; i <= n; i++) // O(n)
for (j = 1; j <= n*3; j++) // n*3 so O(3n) but, since nested it's O(3n^2)
System.out.println("Apple"); // negligible time O(1)
// Total = O(n) + O(3n^2) = O(n^2)
举例5:
for (i = 1; i < n*n*n; i = i*n) // n*n*n = O(n^3) not sure how i = i * n effects things, please help.
System.out.println("Banana"); // negligible time O(1)
// Total = O(n^3) + O(1) = O(n^3)
答案 0 :(得分:0)
实施例5是O(1)
n^3 = ((((1) * n) * n) * n)
- 需要: