大O相加不同的程序时,不同、程序

由网友(世界不会因为你而改变,但你的世界会因为你而改变)分享简介:比方说我有一个扫描的n个元素的整个列表3次常规,不按大小排序,然后bsearches的排序列表n次。该扫描是O(n)的时间,那种我称之为O(N日志(N)),和n次的bsearch是O(n的log(n))。如果加上所有3个在一起,它只是给我们3个的最坏的情况 - 在n的log(n)值(S)还是在语义允许添加的时间Let...

比方说我有一个扫描的n个元素的整个列表3次常规,不按大小排序,然后bsearches的排序列表n次。该扫描是O(n)的时间,那种我称之为O(N日志(N)),和n次的bsearch是O(n的log(n))。如果加上所有3个在一起,它只是给我们3个的最坏的情况 - 在n的log(n)值(S)还是在语义允许添加的时间

Lets say I have a routine that scans an entire list of n items 3 times, does a sort based on the size, and then bsearches that sorted list n times. The scans are O(n) time, the sort I will call O(n log(n)), and the n times bsearch is O(n log(n)). If we add all 3 together, does it just give us the worst case of the 3 - the n log(n) value(s) or does the semantics allow added times?

pretty的肯定,现在我键入此,答案是的n log(n)的,但我还不如现在确认,我把它打出来:)

Pretty sure, now that I type this out that the answer is n log(n), but I might as well confirm now that I have it typed out :)

推荐答案

之和确实是最糟糕的三为大澳的。

The sum is indeed the worst of the three for Big-O.

原因是,你的函数的时间复杂度为

The reason is that your function's time complexity is

(n) => 3n + nlogn + nlogn

这是

(n) => 3n + 2nlogn

此功能的界上面的由3nlogn,所以它在为O(n log n)的。

This function is bounded above by 3nlogn, so it is in O(n log n).

您可以选择任何常数。我刚好选3,因为它是一个很好的渐近上界。

You can choose any constant. I just happened to choose 3 because it was a good asymptotic upper bound.

阅读全文

相关推荐

最新文章