简体   繁体   English

简单的时间复杂度O(nlogn)

[英]simple time complexity O(nlogn)

I am reviewing some Big O notation for an interview and I come across this problem. 我正在审查一些Big O符号进行面试,我遇到了这个问题。

for i = 1 to n do:
    j = i
    while j < n do:
    j = 2 * j

simple right? 简单吧? the outer loop provides n steps. 外循环提供n个步骤。 and each of those steps we do a single step O(1) of assignment j=i then log(nj) or log(ni) since j = i step for the while loop. 并且每个步骤我们执行一个步骤O(1)赋值j=i然后log(nj)或log(ni),因为j = i步骤用于while循环。 I thought the time complexity would be O(nlogn) but the answer is O(n). 我认为时间复杂度是O(nlogn),但答案是O(n)。

here is the answer: 这是答案:

The running time is approximately the following sum: Σ 1 + log(n/i) for i from 1 to n which is Θ(n). 运行时间大约是以下总和:Σ1+ log(n / i),i从1到n,即Θ(n)。

Now it has been a while so I am a bit rusty. 现在已经有一段时间了,所以我有点生疏了。 where does log(n/i) comes from? log(n/i)来自哪里? I know log(n) - log(i) = log(n/i) however I thought we log(ni) not log(n) - log(i). 我知道log(n) - log(i) = log(n/i)但是我认为我们记录(ni)而不是log(n) - log(i)。 and how is the time complexity not O(nlogn)? 时间复杂度如何不是O(nlogn)? I am sure I am missing something simple but I been staring at this for hours now and I am starting to lose my mind. 我相信我错过了一些简单的事情,但我现在已经盯着这几个小时了,我开始失去理智。

source: here is the source to this problem Berkeley CS 170, Fall 2009, HW 1 来源:这是Berkeley CS 170,2009年秋季,HW 1这个问题的根源

edit: after thinking about it a little more it makes sense that the time complexity of the inner loop is log(n/i). 编辑:在考虑了一点之后,内循环的时间复杂度是log(n / i)是有意义的。 cause each inner loop runs ni times but i double each loop. 因为每个内部循环运行ni次,但我每个循环加倍。 if the inner loop were always starting at 0 we have log(n) but take into account the number of the loop we don't have to loop over which is log(i). 如果内部循环总是从0开始,我们有log(n)但是考虑到我们没有循环的循环数,这是log(i)。 log(n) - log(i) which is log(n/i). log(n) - log(i),它是log(n / i)。

I think the log(n/i) comes from the inner loop 我认为log(n / i)来自内循环

notice how j = i 注意j = i

which means when i=2 (lets say n=10) 这意味着当i = 2时(假设n = 10)

the inner loop 内循环

while j < n do:
    j = 2 * j

will run only from j=2 to 10 where j multilplies itself by 2 (hence the log) & quickly overruns the value of n=10 将仅从j=210 ,其中j将自身乘以2(因此记录)并快速超出n=10的值

so the inner loop runs log base 2 n/i times 所以内循环运行log base 2 n/i times

i ran a simple i=10 through the code & it looks like linear time because most of the time inner loop runs only once. 我通过代码运行了一个简单的i = 10,它看起来像线性时间,因为大多数时候内部循环只运行一次。

example : when the value of i becomes such that if you multiply it by 2, you get greater than or equal to n, you don't run the inner loop more than once. 例如:当i的值变为这样,如果将它乘以2,则得到大于或等于n,则不会多次运行内部循环。

so if n=10 you get one execution in the inner loop starting from i=n/2 (if i=10/2=5) then j starts with j=5, gets in the loop once multiplies itself with 2 & the inner loop condition while j < n do: fails. 因此,如果n = 10,你会在内部循环中从i=n/2 (if i=10/2=5)开始执行一次,然后j以j = 5开始,一旦将自身与2和内部相乘而进入循环循环条件, while j < n do:失败。

EDIT : it would be O(n.log(n)) if the value of j started with j=0 everytime & the inner loop ran from i to n 编辑:如果j的值每次从j = 0开始并且内部循环从i运行到n,那么它将是O(n.log(n))

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

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