Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am assuming in this question that n has already been set outside the loop; and by your code will remain constant during execution.</p> <p>Your question is a bit unclear but my understanding is that you want to know the time complexity of the entire code.</p> <p>Let's take a look at the outer loop. Notice that <code>i++</code> increments the loop by 1 each time and the loop goes up to <code>n</code>. Because the loop increments by 1, it is said to be O(n) as the number of iterations grows linearly with the value of <code>n</code>.</p> <p>Next, examine the inner loop. You can see by the lines <code>j = n</code> and <code>while(j &gt;= 1)</code> that the loop begins at n and goes down to 1. Suppose that inside the while loop you had the code <code>j--</code>. This would mean that the number of iterations of the inner loop would increase linearly with <code>n</code>, just as in the outer loop.</p> <p>However, the body of the loop is <code>j = j / 2</code> (assume integer division here, also assuming by your comment that the rest of the body of the loop is O(1) - ie it is inconsequential). Let's have a quick look at some sample values of <code>n</code> for the inner loop:</p> <p><code>n</code> &nbsp; &nbsp; &nbsp; &nbsp; # iterations of while loop</p> <p>1 &nbsp; &nbsp; &nbsp; &nbsp; 1</p> <p>2 &nbsp; &nbsp; &nbsp; &nbsp; 2</p> <p>3 &nbsp; &nbsp; &nbsp; &nbsp; 2</p> <p>4 &nbsp; &nbsp; &nbsp; &nbsp; 3</p> <p>5 &nbsp; &nbsp; &nbsp; &nbsp; 3</p> <p>6 &nbsp; &nbsp; &nbsp; &nbsp; 3</p> <p>7 &nbsp; &nbsp; &nbsp; &nbsp; 3</p> <p>8 &nbsp; &nbsp; &nbsp; &nbsp; 4</p> <p>9 &nbsp; &nbsp; &nbsp; &nbsp; 4</p> <p>10 &nbsp; &nbsp; &nbsp; &nbsp; 4</p> <p>11 &nbsp; &nbsp; &nbsp; &nbsp; 4</p> <p>12 &nbsp; &nbsp; &nbsp; &nbsp; 4</p> <p>13 &nbsp; &nbsp; &nbsp; &nbsp; 4</p> <p>14 &nbsp; &nbsp; &nbsp; &nbsp; 4</p> <p>15 &nbsp; &nbsp; &nbsp; &nbsp; 4</p> <p>....</p> <p>You can probably see the pattern emerging - one 1, two 2s, four 3s, eight 4s. These are powers of 2. So given the value of <code>n</code> we can determine the number of iterations:</p> <p><code>iterations = FLOOR(Log base 2(n)) + 1</code>.</p> <p>We need to use <code>FLOOR</code> to simulate the integer division, and the <code>+ 1</code> takes care of the fact that we loop while <code>j</code> is greater than or <b>equal to</b> 1.</p> <p>So the number of iterations grows by Log base 2 of n. The 1 is not relevant here because we are interested in big-O notation where constant multiples or additions have no effect on the growth of one function <b>relative to the growth of another function</b>.</p> <p>So to summarize so far: the outer loop is growing at rate <b>O</b>(n) while the inner loop is growing at <b>O</b>(Log base 2 n) <i>(which is normally written as just Log(n))</i>.</p> <p>Now, when dealing with nested loops, the complexity of the total code is found by multiplying the complexities of the nested loops. This is because for every iteration of the outer loop, we must complete Log(n) inner loops. (See <a href="https://stackoverflow.com/questions/766939/finding-big-o-with-multiple-nested-loops">this question</a> for more info).</p> <p>So now you can see that the total complexity of the snippet you have provided is <code>n * Log(n)</code>.</p> <p>If you want to read more about big-O, see <a href="http://courses.csail.mit.edu/6.01/spring07/lectures/lecture4.pdf" rel="nofollow noreferrer">here</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload