简体   繁体   English

以下功能的主要功能是什么:

[英]What is the Big O for the following functions:

I need help with the following problems on determining what the Big O is of each function. 在确定每个函数的Big O是什么时,我需要以下问题的帮助。

For problem one, I've tried O(log(n)) and O(n). 对于问题一,我尝试了O(log(n))和O(n)。 I figured the function was linear or in other words, for N elements we will require N iterations. 我认为函数是线性的,换句话说,对于N个元素,我们将需要N次迭代。

For problem two, I've tried O(n^2). 对于第二个问题,我尝试了O(n ^ 2)。 I figured for this kind of order, the worst case time (iterations) is the square of the number of inputs. 我认为对于这种顺序,最坏的情况下的时间(迭代次数)是输入数量的平方。 The time grows exponentially related to the number of inputs. 时间与输入数量成指数关系增长。

For problem three, I've tried O(n^2) and O(1). 对于问题三,我尝试了O(n ^ 2)和O(1)。

Problem One: 问题一:

function foo(array){
  let sum = 0;
  let product = 1;
  for (let i = 0; i < array.length; i++){
    sum += array[i]
  }
  for(let i = 0; i < array.length; i++){
    product *= array[i];
  }

  consle.log(sum + ", " + product);
}

Problem Two: 问题二:

function printUnorderedParis(array){
  for(let i = 0; i < array.length; i++){
    for(let j = i + j; j < array.length; j++){
      console.log(array[i] + ", " + array[j]);
    }
  }
}

Problem Three: 问题三:

function printUnorderedPairs(arrayA, arrayB){
  for(let i = 0; i < arrayA.length; i++){
    for(let j = 0; i < arrayB.length; j++){
      for(let k = 0; k < 100000; k++){
        console.log(arrayA[i] + ", " + arrayB[j]);
      }
    }
  }
}

I expected my initial thoughts to be right, but maybe I'm having a hard time grasping Big O. 我希望最初的想法是正确的,但也许我在把握Big O时遇到了困难。

  1. You're correct that it's O(n) . 您是正确的,它是O(n) You have two loops, they each perform array.length iterations. 您有两个循环,它们每个都执行array.length迭代。 You could even combine them into a single loop to make it more obvious. 您甚至可以将它们组合成一个循环,使其更加明显。

     for (let i = 0; i < array.length; i++) { sum += array[i]; product *= array[i]; } 
  2. You're correct, it's O(n^2) . 您是正确的,它是O(n^2) The nested loops perform array.length * array.length iterations. 嵌套循环执行array.length * array.length迭代。
    EDIT -- see my comment above asking whether this problem is copied correctly. 编辑-请参阅上面的我的评论,询问是否正确复制了此问题。

  3. This is also O(n^2) . 这也是O(n^2) The third level of nested loop doesn't change the complexity, because it performs a fixed number of iterations. 嵌套循环的第三级不会改变复杂性,因为它执行固定数量的迭代。 Since this doesn't depend on the size of the input, it's treated as a constant. 由于这不取决于输入的大小,因此将其视为常量。 So as far as Big-O is concerned, this is equivalent to Problem 2. 因此,就Big-O而言,这等效于问题2。

Well, you kind of answered your questions, but here we go: 好吧,您有点回答了您的问题,但是我们开始:

  1. In the first problem, you have two for loops, each of them iterating over the entire array. 在第一个问题中,您有两个for循环,每个循环遍历整个数组。 For a general array of size n, you'll have O(2n) or simply O(n) since we can let go of constants. 对于大小为n的通用数组,您将拥有O(2n)或仅拥有O(n)因为我们可以放开常量。 There isn't any reasons this would be O(log(n)) . 没有任何理由会是O(log(n))

  2. For the second one, I think there is a mistake. 对于第二个,我认为有一个错误。 The statement j = i + j is not valid, and you'll get Uncaught ReferenceError: j is not defined . 语句j = i + j无效,您将得到Uncaught ReferenceError: j is not defined However, let's say the statement is actually let j = i . 但是,假设该语句实际上是let j = i Then, we have: 然后,我们有:

    • i, iterating over the entire array, starting from the first element and going all the way to the last one i,遍历整个数组,从第一个元素开始,一直到最后一个
    • j, starting from i and going all the way to the last element j,从i开始一直到最后一个元素

With this information, we know that for i = 0 , j will iterate from 0 to n (n being the array's length), so n steps. 有了这些信息,我们知道对于i = 0 ,j将从0迭代到n (n是数组的长度),因此n步。 For i=1 , j will go from 1 to n, so n-1 steps. 对于i=1 ,j将从1变为n,因此n-1步。 Generalizing, we are going to have a sum: n + (n - 1) + (n - 2) + ... + 1 + 0 = n * (n + 1) / 2 = 1/2 * (n^2 + n) . 概括地说,我们将得到一个总和: n + (n - 1) + (n - 2) + ... + 1 + 0 = n * (n + 1) / 2 = 1/2 * (n^2 + n) So, the complexity is O(1/2 * (n^2 + n) = O(n^2 + n) = O(n) . So you were correct. 因此,复杂度为O(1/2 * (n^2 + n) = O(n^2 + n) = O(n)

  1. For the third problem, the answer is O(n^2) and not O(1). 对于第三个问题,答案是O(n ^ 2)而不是O(1)。 The reasoning is very close to the one I made for the second one. 推理与我为第二个推理所做的非常接近。 Basically, the inner k will be executed 100000 times for every j iteration, but the number of iteration does not depend of n (the size of the array). 基本上,内部k将为每个j迭代执行100000次,但是迭代次数不取决于n (数组的大小)。

It's easy to see that: 很容易看到:

  1. for i = 0 , j will go from 0 to n (last value for which j body will be executed being j = n - 1 ). 对于i = 0 ,j从0到n (将执行j body的最后一个值为j = n - 1 )。

    • for j = 0 , we will to 100k iterations 对于j = 0 ,我们将进行100k次迭代
    • for j = 1 , another 100k iterations 对于j = 1 ,再进行100k次迭代
    • ... ...
    • for j = n - 1 , another 100k iterations 对于j = n - 1 ,又进行了100k次迭代

The entire j loop will make n * 100000 = 100000n iterations. 整个j循环将进行n * 100000 = 100000n次迭代。

  1. For i = 1 , the same behaviour: 对于i = 1 ,相同的行为:
    • for j = 0 , we will to 100k iterations 对于j = 0 ,我们将进行100k次迭代
    • for j = 1 , another 100k iterations 对于j = 1 ,再进行100k次迭代
    • ... ...
    • for j = n - 1 , another 100k iterations, 对于j = n - 1 ,又进行了100k次迭代,

getting another 100000n iterations. 100000n一次100000n次迭代。

In the end, we end up with 100000n + 100000n + ... + 100000n (n times) = sum(i = 0, n, 100000n) = n * 100000n = 100000 * n^2 . 最后,我们得到100000n + 100000n + ... + 100000n (n times) = sum(i = 0, n, 100000n) = n * 100000n = 100000 * n^2 So, the big O is O(100000 * n^2) = O(n^2) . 因此,大O为O(100000 * n^2) = O(n^2)

Cheers! 干杯!

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

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