简体   繁体   English

N log(N)或N澄清

[英]N log(N) or N clarification

Will performing a O(log N) algorithm N times give O(N log(N))? 将O(log N)算法执行N次会得到O(N log(N))吗? Or is it O(N)? 还是O(N)?

eg Inserting N elements into a self-balancing tree. 例如,将N个元素插入自平衡树。

int i = 0;
while (i++ < N) {
    insert(itemsToInsert[i]);
}

It's definitely O(N log(N)). 绝对是O(N log(N))。 It COULD also be O(N), if you could show that the sequence of calls, as a total, grows slow enough (because while SOME calls are O(log N), enough others are fast enough, say O(1), to bring the total down). 如果您可以证明调用顺序总体上增长得足够慢,则它也可以是O(N)(因为某些调用为O(log N),而其他调用足够快,例如O(1),降低总数)。

Remember: O(f) means the algorithm is no SLOWER than f, but it can be faster (even if just in certain cases). 请记住:O(f)表示算法比f慢,但是它可以更快(即使在某些情况下)。

N次O(log(N))导致O(N log(N))。

Big-O notation notates the asymptotic behavior of the algorithm. Big-O表示法表示算法的渐近行为。 The cost of each additional step is O(log N) ; 每增加一个步骤的成本为O(log N) we know that for an O(N) algorithm, the cost of each additional step is O(1) and asymptotically the cost function bound is a straight line. 我们知道,对于O(N)算法,每个附加步骤的成本为O(1)并且渐近而言,成本函数的边界为一条直线。

Therefore O(N) is too low of a bound; 因此, O(N)太低了。 O(N log N) seems about right. O(N log N)似乎正确。

Yes and no. 是的,没有。

Calculus really helps here. 微积分在这里确实有帮助。 The first iteration is complexity log(1), the second iteration is log(2), &ct until the Nth iteration which is log(N). 第一次迭代是复杂度log(1),第二次迭代是log(2),&ct直到第N次迭代是log(N)。 Rather than thinking of the problem as a multiplication, think of it as an integral... 与其将问题视为一个乘法,不如将其视为一个不可或缺的...

在此处输入图片说明

This happens to come out as O(N log(N)), but that is kind of a coincidence. 碰巧是O(N log(N)),但这是一个巧合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM