0

I am very much confuse that why complexity of this program is O(n)?

 int j = 0;
 int i;
 For (i = 0; i < n, i++) // O(n)
 {
      For (i = 0; i < n, i++)//O(n)
      {
        While (j < n)// O(n)
       {
        Statement; j++;
       }
   }    
 }

I am totally new to Algorithms. Any help and explanation will be appreciated.

  • First of all please replace the image with text (source code). Second, what is your version? Why do you think it should be $O(n^3)$ and not $O(n^)$? Please include it in your post. – fade2black Jan 03 '18 at 18:34
  • @fade2black I tried to type text but it gets jumbled up. I don't know how to use editor, I am completely new here. And how I thought it is O(n^3) , there are three loops, so obvious it is O(n^3) –  Jan 03 '18 at 18:39
  • 1
    In fact $O(n^3)$ is not a wrong answer, but $O(n)$ is tighter bound. Three loops does not necessary imply cubic time. Please pay attention to variables used in the for-loops. In particular, is it true that the outermost for-loop always runs $n$ times? How many times is j++ invoked? – fade2black Jan 03 '18 at 18:49
  • @fade2black sorry to say, I tried. Inner loop runs n times. Outer For loop will executed once and later i will be incremented one more time before it get terminated. J++ runs n times, only once. But I want some concrete answer :/ –  Jan 03 '18 at 18:52
  • The inner for-loop starts from $i=0$ and runs until $i=n$.Statement; j++ is executed $n$ times for only $i=0$ and never executed for $i>0$. The outer for-loop runs only once for $i=0$. You could observe it by running this code. – fade2black Jan 03 '18 at 19:00
  • So, complexity would be O(n^2)? –  Jan 03 '18 at 19:03
  • Statement; j++ is invoked $n$ times (total!), and the second for-loop increments $i$ $n$ times (total!), and the outer for-loop increments $i$ only once after which it quits since $i$ reaches $n$ (by the second for-loop). This gives $n+n +1= 2n+1$ which is $O(n)$. – fade2black Jan 03 '18 at 19:08
  • Take n=5 and execute the algorithm on paper, step by step. Observe. – Raphael Jan 03 '18 at 19:12
  • @fade2black I wanted to say O (n) but editor says I can edit only after 5 mins. I already understand, Thanks!!! –  Jan 03 '18 at 19:14
  • @Raphael sure I will :) –  Jan 03 '18 at 19:14
  • @Raphael How can you mark this question as duplicate? It is a specific question. You can't link my specific question to your generalized question. It is against guidelines. –  Jan 03 '18 at 19:35
  • @ubhatt Your question contains little but code and the prompt "please analyse for me". That's what we call a problem dump. And since the reference question I link explains in great detail how to deal with loops, it is a duplicate. Now, this may be different if you included your previous attempts and thoughts in the question, and ask an actual, narrow question. But you don't. So a duplicate it is. (And trust me, I'm very aware of the guidelines.) – Raphael Jan 03 '18 at 19:42
  • I'll just give you a hint: There are three loops in your code. And two of them are cleverly designed to convince an inexperienced programmer that they are doing something totally different from what they are actually doing. Examine the code very carefully. You should twice say to yourself "NOW I get what it does. This was hidden really cleverly". Then the answer to your question is really simple. – gnasher729 Jan 03 '18 at 22:53

0 Answers0