简体   繁体   English

如何找到while循环的时间复杂度(Big O)?

[英]How do I find the time complexity (Big O) of while loop?

Code 1代码 1

int i = 0;
int j = 0;
while(i < n){
     while(j < n){
        printf("{%d,%d}",arr[i],arr[j]);
        j++;
    }
    i++;
    j = 0;
    printf("\n");
}

Code 2代码 2

int result = 0;
int i = 0;
while (i < n / 2){
    result += arr[i];
    i += 1;
    while (i >= n / 2 && i < n){
        result += arr[i];
        i += 1;
    }
}
printf("%d\n", result);

I only know how to find time complexity with for loops, but I am uncertain about while loop.我只知道如何使用 for 循环找到时间复杂度,但我不确定 while 循环。 It would be greatly appreciated if someone could help me find the total running time of each code.如果有人能帮我找到每个代码的总运行时间,将不胜感激。

The first code sample is pretty much a classis for loop.第一个代码示例几乎是 for 循环的分类。 Its complexity is O(n^2).它的复杂度是 O(n^2)。 This is because the inner loop has a complexity O(n) and it is run n times.这是因为内部循环的复杂度为 O(n) 并且它运行了 n 次。

The second one is a bit more difficult, untill you see that is equivalent to a non nested loop (ignoring the complexity of the checks)第二个有点困难,直到你看到它相当于一个非嵌套循环(忽略检查的复杂性)

int result = 0;
int i = 0;
while (i < n){
    result += arr[i];
    i += 1;
}
printf("%d\n", result);

meaning its complexity is O(n)意味着它的复杂度是 O(n)

The best approach to calculating time complexity is trying to actually understand how the algorithm works and counting the operations.计算时间复杂度的最佳方法是尝试真正了解算法的工作原理并计算操作数。 In the second example, the inner loop never runs untill the outer loop is at its last iteration.在第二个示例中,在外循环进行最后一次迭代之前,内循环永远不会运行。 And since they even execute the same code, the whole thing can be reduced to one loop.而且由于它们甚至执行相同的代码,因此整个过程可以减少为一个循环。

Another good example is this:另一个很好的例子是这样的:

for(i=0;i<n;i++){
    for(j=i;j<n;i++){
        //do something
    }
}

Let's count the operations: 1 + 2 + 3 + 4 + ... + n.让我们计算一下操作:1 + 2 + 3 + 4 + ... + n。 This comes down to n*n/2 leading to O(n^2)这归结为 n*n/2 导致 O(n^2)

A for loop, at the end of the day, IS a while loop.一个 for 循环,归根结底是一个 while 循环。 Something of the form:形式的东西:

for(int i=0; i<n; i++)

is equivalent to:相当于:

int i=0;

while(i<n)
{
 i++;
}

In fact in pure mathematical analysis of algorithms you are supposed to make any for loop into a while loop in your algorithms (there are a couple reasons why).事实上,在算法的纯数学分析中,您应该在算法中将任何 for 循环转换为 while 循环(有几个原因)。

Going back to your code.回到你的代码。 The analysis is simple:分析很简单:

  • Before the first iteration of the while loop the value of i is 0.在 while 循环的第一次迭代之前,i 的值为 0。
  • There exists a unique statement that updates the variable i (i++).存在更新变量 i (i++) 的唯一语句。
  • On every iteration of the outer loop i increases by 1.在外循环的每次迭代中,i 增加 1。
  • The outer loop runs at most n times.外循环最多运行 n 次。

  • Before the iteration of any loop the value of j is 0.在任何循环的迭代之前,j 的值为 0。

  • There are 2 statements that update j (j=0 and j++)有 2 条语句更新 j(j=0 和 j++)
  • Informally: we can tell before any iteration of the inner loop the value of j is 0.非正式地:我们可以在内循环的任何迭代之前知道 j 的值是 0。

  • Inside the inner loop the only statement that updates j is j++.在内部循环中,更新 j 的唯一语句是 j++。

  • on every iteration of the inner loop j increases by 1.在内部循环的每次迭代中,j 增加 1。
  • The inner loop runs at most n times by the loop guard.内部循环最多由循环保护运行 n 次。

  • The outer loop runs at most n times, the inner loop runs at most n times for every iteration of the outer loop.对于外循环的每次迭代,外循环最多运行 n 次,内循环最多运行 n 次。 All other statements are constant.所有其他语句都是不变的。 The algorithm is in O(n*n)=O(n^2)算法在 O(n*n)=O(n^2)

The second one is slightly more convoluted but:第二个稍微复杂一些,但是:

  • Before the first iteration of the outer loop the value of i is 0.在外循环的第一次迭代之前,i 的值为 0。
  • Informally: the outer loop will run until the value of i is (n/2 - 1)非正式地:外循环将运行直到 i 的值为 (n/2 - 1)
  • The update statement (i += 1) updates i to (n/2)更新语句 (i += 1) 将 i 更新为 (n/2)
  • Informally: the inner loop runs (nn/2 = n/2) times and it runs exactly once.非正式地:内循环运行 (nn/2 = n/2) 次并且它只运行一次。 -The outer loop runs n/2 times, the inner loop runs n/2 times exactly once. - 外循环运行 n/2 次,内循环运行 n/2 次正好一次。

The algorithm thus runs O(n/2+n/2) = O(n) times该算法因此运行 O(n/2+n/2) = O(n) 次

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

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