查找函数的最坏情况运行时

时间:2017-01-16 20:08:41

标签: algorithm math big-o

我真的不明白下一个问题要去哪里。

非常感谢任何提示或步骤。

enter image description here

我真的想知道我应该做什么,而不是仅仅得到答案。

我理解为什么我们使用Big-Oh(最糟糕的情况),但我无法将我的思想包含在数学背后。如何计算总运行时间?

1 个答案:

答案 0 :(得分:0)

要计算Big-Oh表示法,您需要找到最差的运行时间。 我们为三个循环执行此操作

1) i varies from 1 to n-1  so total times the loop runs n-1=O(n)
2) j goes from 2 to n,3 to n..............n-1 to n so here total times the loop runs (n-2)+(n-3).......+1=(n-1)(n-2)/2=O(n^2)
3) now for each  value of j say a. k goes from 1 to a . This will add up another O(n) for each loop.(as value of a can go upto n)

Conclusion
Total loops of j O(n^2) and inside each loop we have O(n) operation.
Therefore O((n^2)*n)=O(n^3) 

希望我能够解释我们如何获得Big-Oh符号