2

What is the complexity for the following piece of code, O(logn) or O(nlogn)?

int i=1; 
while (i<= n) { 
   int j = i; 
   while (j > 0) { 
      j = j/2;
   } 
   i++;  
}

I asked chatgpt and sometimes it says O(logn) and sometimes O(nlogn). The argument for O(logn) is that the inner loop dominates the number of iterations and that it should be taken, and the argument for O(nlogn) (which I find more reasonable) is that this is the sum of logarithms from 1 to n, which is logn!, which can be approximated to O(nlogn). I also saw this stackoverflow question and one of the answers is O(nlogn) and the other O(logn).

Silidrone
  • 1,471
  • 4
  • 20
  • 35
  • *O(nlogn)*. The intuition you described is already good (the intuition for *O(logn)* is just bs). But as a sanity check, you can always actually run this code and count how many times the while loop executes. You will find that it is definitely not less than *n*, so you can rule out *O(log n)* right away. Are you looking for a way to write a formal proof on it or is the intuition sufficient for you? – Berthur Mar 06 '23 at 21:17
  • @Berthur The intuition is enough, but I do not understand the "inner loop dominates the number of iterations" logic that chatgpt provided. It said something like: "as the input size approaches infinity, the number of inner loop's iterations is far greater than the out, so it dominates and we should only focus on the inner loop". Which sounds completely wrong as the inner loop's number of iterations are directly related with the outer. I guess I am looking more for a disproof of the alternative theory, rather than a proof for the initial one, as it sounds correct by intuition. – Silidrone Mar 07 '23 at 15:34
  • 1
    You may compose your comments into an answer so the future visitors can see the answer, I'll accept it. – Silidrone Mar 08 '23 at 14:30

1 Answers1

1

It is O(n logn).

It is easy to disprove O(logn): The outer loop executes exactly n times, so the algorithm must be Ω(n). At this point it doesn't matter even if the inner loop would execute 0 times because it is still a constant-time operation.

Proving that it is indeed O(n logn) is a bit more tricky and involves some math, but that's not really what you are asking so I won't get into that. But in cases like this, you can perform a very easy sanity check, by just counting the number of iterations the inner loop will perform:

int i=1;
counter = 0;
while (i<= n) { 
   int j = i; 
   while (j > 0) { 
      j = j/2;
      counter++;
   } 
   i++;  
}

The value of counter for different inputs of n will answer your question in practice, even if it's not a formal proof.

As for ChatGPT, it is important to remember that it's far from an authority, so I would disregard it and not think too much about how it might have picked up that argument about O(log n). If we must speculate, then I suppose ChatGPT might have learned that argument from other contexts, such as when you have two parallel (not nested) loops where one takes more time than the other. Then, the slower one will dominate and let you ignore the faster one. But obviously that argument is applied incorrectly here. So yes, your own logic seems fine to me.

Berthur
  • 4,300
  • 2
  • 14
  • 28