-1

Having different loops inside of a function doesn't cause the BigOh to be multiplied together, right?

Example:

function() {
    for(int i = 0; i < n; i++) {
        //logic here
    }

    for(int i = 0; i < n; i++) {
        //logic here
    }
}
betabandido
  • 18,946
  • 11
  • 62
  • 76
shaboinkin
  • 171
  • 1
  • 11

2 Answers2

1

This is pretty well discussed (i.e. General Reference) but yes you are correct, the function you have in your question would be O(n).

Technically O(2n) which gets reduced to O(n)

NominSim
  • 8,447
  • 3
  • 28
  • 38
1

Yes it's still O(n) because you would have O(n+n) which is O(2n) but we can ignore the 2 because it has negligible effect. But if you had

for (...){
  for(...){
    //code here
  }
}

Then it would be O(n^2)

Ryan
  • 1,797
  • 12
  • 19
  • What happens if the number of iterations in both loops are different? For example, first loop is iterated `m` times and second one is iterated `n` times where `n>>m`. – santobedi May 20 '18 at 01:46