我在計算迭代依賴於外部循環的兩組代碼示例的Big O運行時遇到了一些麻煩。我對Big O運行時間有了基本的瞭解,並且我可以計算出更簡單的代碼示例的運行時間。我不太確定某些線路是如何影響運行時間的。嵌套循環的大O,有不同的迭代?
我會考慮這第一個O(n^2)。但是,我不確定。
for(i = 1; i < n; i++){
for(j = 1000/i; j > 0; j--){ <--Not sure if this is still O(n)
arr[j]++; /* THIS LINE */
}
}
我對這個有點失落。 O(n^3)可能是O(n^2)?
for(i = 0; i < n; i++){
for(j = i; j < n; j++){
while(j<n){
arr[i] += arr[j]; /* THIS LINE */
j++;
}
}
}
我發現這個職位和我申請這第一個代碼示例,但我仍然不能確定第二。 What is the Big-O of a nested loop, where number of iterations in the inner loop is determined by the current iteration of the outer loop?
這應該是公認的答案OP對問題1的錯誤回答。 – meowgoesthedog
@meowgoesthedog同意 – UnknowableIneffable