简体   繁体   中英

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.

For problem one, I've tried O(log(n)) and O(n). I figured the function was linear or in other words, for N elements we will require N iterations.

For problem two, I've tried 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).

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.

  1. You're correct that it's O(n) . You have two loops, they each perform array.length iterations. 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) . The nested loops perform array.length * array.length iterations.
    EDIT -- see my comment above asking whether this problem is copied correctly.

  3. This is also 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.

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 a general array of size n, you'll have O(2n) or simply O(n) since we can let go of constants. There isn't any reasons this would be 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 . However, let's say the statement is actually 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
    • j, starting from i and going all the way to the last element

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. For i=1 , j will go from 1 to n, so n-1 steps. Generalizing, we are going to have a sum: 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.

  1. For the third problem, the answer is O(n^2) and not 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).

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 ).

    • for j = 0 , we will to 100k iterations
    • for j = 1 , another 100k iterations
    • ...
    • for j = n - 1 , another 100k iterations

The entire j loop will make n * 100000 = 100000n iterations.

  1. For i = 1 , the same behaviour:
    • for j = 0 , we will to 100k iterations
    • for j = 1 , another 100k iterations
    • ...
    • for j = n - 1 , another 100k iterations,

getting another 100000n iterations.

In the end, we end up with 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) .

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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