3
s=0; 
for(i=1;i<n;i=i*2) 
{ 
    for(j=0;j<n;j++)
    {
        s=s+i*j;
    } 
    s=s+1;
}

I'm trying to establish the big-o complexity for the above algorithm. The outer loop starts at 1 and runs to n, counter in i doubles for each iteration, thus this is a log(n) behaviour. The inner loop runs from 0 to n with O(n) behaviour. I'm confused whether it is O(n * log(n)) or not, the order matters or not? Also j starts at 0, not at 1 so this can't be O(n * log(n))?

Alina Khachatrian
  • 757
  • 1
  • 6
  • 19
  • 2
    Replace you assignments to `s` with `s += 1`; the value of `s` at the end is what your O() should represent. That might help you decide if the order matters. (Note that for some code it does, whether or not it does in this instance.) – Scott Hunter Jun 03 '18 at 17:46
  • Look at this answer: https://stackoverflow.com/a/19021326/3160529 – Shubham Jun 03 '18 at 18:02

3 Answers3

3

In this case, the inner loop is independent of the outer loop. So we can just count the number of times outer loop runs and then multiply it with the number of times inner loop runs and that will be the complexity.

No. of times outer loop runs is log2(n) as the number is being multiplied by 2 each time.

No. of times inner loop runs is always n.

Thus, complexity will be O(nlog2(n)).

Shubham
  • 2,847
  • 4
  • 24
  • 37
3

You can actually count how many times the inner loop runs in a simple case like this. It will give a good indication of the asymptotic behavior as n changes. Here's a quick javascript example:

function count(n) {
    let count=0; 
    for(i=1; i<n; i=i*2) 
    { 
        for(j=0;j<n;j++)
        {
            count++
        } 
       
    }    
    return count
}
let n = 1024
console.log("n:", n, "iterations:", count(n),"nlog(n):", Math.log2(n) * n)

n = 32768
console.log("n:", n, "iterations:", count(n),"nlog(n):", Math.log2(n) * n)

n = 1048576
console.log("n:", n, "iterations:", count(n),"nlog(n):", Math.log2(n) * n)

The behavior looks like O(nlog(n)) to me. Additionally it stands to reason that performing log(n) loops that each perform n iterations will be O(nlog(n)) because n * log(n) === log(n) * n

Mark
  • 90,562
  • 7
  • 108
  • 148
2

I'm confused whether it is O(n * log(n)) or not

Yes, you are correct. Just like you have explained, there are log(n) iterations of the outer loop and n iterations of the inner loop, so O(log(n) * n) total complexity.

the order matters or not?

Does not matter here. e.g. O(log(n) * n) == O(n * log(n))

Also j starts at 0, not at 1 so this can't be O(n * log(n))

Will not affect the complexity. When N goes to infinity, starts at 0 or starts at 1 makes no difference.

pjs
  • 18,696
  • 4
  • 27
  • 56
gchen
  • 1,173
  • 1
  • 7
  • 12