How do I find the time complexity (Big O) of while loop?

40,096

Solution 1

The first code sample is pretty much a classis for loop. Its complexity is O(n^2). This is because the inner loop has a complexity O(n) and it is run n times.

The second one is a bit more difficult, untill you see that is equivalent to a non nested loop (ignoring the complexity of the checks)

int result = 0;
int i = 0;
while (i < n){
    result += arr[i];
    i += 1;
}
printf("%d\n", result);

meaning its complexity is O(n)

The best approach to calculating time complexity is trying to actually understand how the algorithm works and counting the operations. In the second example, the inner loop never runs untill the outer loop is at its last iteration. And since they even execute the same code, the whole thing can be reduced to one loop.

Another good example is this:

for(i=0;i<n;i++){
    for(j=i;j<n;i++){
        //do something
    }
}

Let's count the operations: 1 + 2 + 3 + 4 + ... + n. This comes down to n*n/2 leading to O(n^2)

Solution 2

A for loop, at the end of the day, IS a while loop. Something of the form:

for(int i=0; i<n; i++)

is equivalent to:

int i=0;

while(i<n)
{
 i++;
}

In fact in pure mathematical analysis of algorithms you are supposed to make any for loop into a while loop in your algorithms (there are a couple reasons why).

Going back to your code. The analysis is simple:

  • Before the first iteration of the while loop the value of i is 0.
  • There exists a unique statement that updates the variable i (i++).
  • On every iteration of the outer loop i increases by 1.
  • The outer loop runs at most n times.

  • Before the iteration of any loop the value of j is 0.

  • There are 2 statements that update j (j=0 and j++)
  • Informally: we can tell before any iteration of the inner loop the value of j is 0.

  • Inside the inner loop the only statement that updates j is j++.

  • on every iteration of the inner loop j increases by 1.
  • The inner loop runs at most n times by the loop guard.

  • The outer loop runs at most n times, the inner loop runs at most n times for every iteration of the outer loop. All other statements are constant. The algorithm is in O(n*n)=O(n^2)

The second one is slightly more convoluted but:

  • Before the first iteration of the outer loop the value of i is 0.
  • Informally: the outer loop will run until the value of i is (n/2 - 1)
  • The update statement (i += 1) updates i to (n/2)
  • Informally: the inner loop runs (n-n/2 = n/2) times and it runs exactly once. -The outer loop runs n/2 times, the inner loop runs n/2 times exactly once.

The algorithm thus runs O(n/2+n/2) = O(n) times

Share:
40,096
stanley cho
Author by

stanley cho

Updated on January 04, 2020

Comments

  • stanley cho
    stanley cho over 4 years

    Code 1

    int i = 0;
    int j = 0;
    while(i < n){
         while(j < n){
            printf("{%d,%d}",arr[i],arr[j]);
            j++;
        }
        i++;
        j = 0;
        printf("\n");
    }
    

    Code 2

    int result = 0;
    int i = 0;
    while (i < n / 2){
        result += arr[i];
        i += 1;
        while (i >= n / 2 && i < n){
            result += arr[i];
            i += 1;
        }
    }
    printf("%d\n", result);
    

    I only know how to find time complexity with for loops, but I am uncertain about while loop. It would be greatly appreciated if someone could help me find the total running time of each code.