简体   繁体   English

我不明白如何计算此算法的时间复杂度

[英]I don't understand how the time complexity for this algorithm is calculated

int j=0;
for (int i=0; i<N; i++) 
{
    while ( (j<N-1) && (A[i]-A[j] > D) )
        j++;
    if (A[i]-A[j] == D) 
        return 1; 
}

This code is said to have the time complexity of O(n), but I don't really get it. 据说这段代码的时间复杂度为O(n),但我并没有真正得到它。 The inner loop is executed N times and the outer should be also N times? 内循环执行N次,外部也应该N次? Is it maybe because of the j = 0; 是因为j = 0; outside the loop that is making it only run N times? 在循环之外使它只运行N次?

But even if it would only run N times in the inner loop, the if statment check should be done also N times, which should bring the total time complexity to O(n^2)? 但即使它只在内循环中运行N次,if语句检查也应该进行N次,这应该将总时间复杂度带到O(n ^ 2)?

The reason why this is O(n) is because j is not set back to 0 in the body of the for loop. 这是O(n)的原因是因为在for循环的主体中j 没有被设置回0

Indeed if we take a look at the body of the for loop, we see: 实际上,如果我们看一下for循环的主体,我们会看到:

while ( (j<N-1) && (A[i]-A[j] > D) )
    j++;

That thus means that j++ is done at most n-1 times, since if j succeeds N-1 times, then the first constraint fails. 这意味着j++最多完成n-1次,因为如果j成功N-1次,则第一个约束失败。

If we take a look at the entire for loop, we see: 如果我们看一下整个for循环,我们会看到:

int j=0;
for (int i=0; i<N; i++) {
    while ( (j<N-1) && (A[i]-A[j] > D) )
        j++;
    if (A[i]-A[j] == D) 
        return 1;
}

It is clear that the body of the for loop is repeated n times, since we set i to i=0 , and stop when i >= N , and each iteration we increment i . 很明显, for循环的主体重复n次,因为我们将i设置为i=0 ,并且当i >= N时停止,并且每次迭代我们增加i

Now depending on the values in A we will or will not increment j (multiple times) in the body of the for loop. 现在,根据A的值,我们将在或不会在for循环体中增加j (多次)。 But regardless how many times it is done in a single iteration, at the end of the for loop, j++ is done at most n times, for the reason we mentioned above. 但无论在一次迭代中完成多少次,在for循环结束时, j++最多都会完成n次,这是我们上面提到的原因。

The condition in the while loop is executed O(n) (well at most 2×n-1 times to be precise) times as well: it is executed once each time we enter the body of the for loop, and each time after we execute a j++ command, but since both are O(n) , this is done at most O(n+n) thus O(n) times. while循环中的条件也执行O(n) (最多精确2×n-1次)次数:每次进入for循环体时执行一次,每次执行后我们执行一次执行一个j++命令,但因为两者都是O(n) ,所以这最多是O(n + n),因此是O(n)次。

The if condition in the for loop executed n times: once per iteration of the for loop, so again O(n) . 所述if条件在for循环执行n次:一次每次迭代的for循环,所以再次为O(n)。

So this indeed means that all "basic instructions" ( j++ , i = 0 , j = 0 , j < N-1 , etc.) are all done either a constant number of times O(1) , or a linear number of times O(n) , hence the algorithm is O(n) . 所以这确实意味着所有“基本指令”( j++i = 0j = 0j < N-1等)都是以常数O(1)或线性次数完成的O(n) ,因此算法是O(n)

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

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