Home > Mobile >  I want to find the asymptotic complexity f(n) for the following C code
I want to find the asymptotic complexity f(n) for the following C code

Time:10-03

for(int i=0; i<rows; i  )
{
    for(int j=0; j<cols; j  )
    {
        statement;
    }
}

I observed that the outer loop runs rows time and the inner loop runs rows * cols times and the statement runs rows*cols times as well. How should I put all them together to find a general function f(n) to find the number of steps the whole code runs so that I could find its lower and upper bounds.

CodePudding user response:

Complexity of this code is O(n²). But in your case complexity is counted as a number of steps inside the code. So if you have rows and columns (a matrix actually) then complexity is just a product of members rows*cols.

In more detailed way its better to count all the code that is executed in your statement. But for the approximation method above is good.

P.S. To see the bounds of the code its useful to look at the graphics of the functions like f(x)=x² (in your case).

  • Related