Time complexity on nested for loop

Brody_Brody
function(n): 
{
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n / 2; j++)
            output("")
    }
}

Now I have calculated the time complexity for the first for loop which is O(n). Now the second for loop shows j <= n / 2 so any given n I put, for example, a range of [1,2,...,10] will give me O(log(n)) since it will continuously give me a series of n,n/2,n/4,n/8 .... K.

So if we wanted to compare the relationship it must look something like this 2^n = k.

My question is will it give me O(log(n))?

abc

No, it does not give you o(logn).
The first for loop is O(n). The second loop is O(n) as well, as the number of iterations grows as a function of n (the growth rate is linear).
It would be the same even by changing the second loop to something like

for (j=1; j<=n/2000; j++)

or in general if you replace the denominator with any constant k.
To conclude, the time compexity is quadratic, i.e., O(n^2)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Time complexity nested loop

Time complexity of nested for loop

Time complexity for a nested for loop

Time Complexity - nested for loop

Time complexity of an unorthodox nested for loop

reduce the time complexity of nested for loop

Average Time Complexity of Nested Loop

Time complexity of a triple nested for loop

Time complexity of nested while loop

Time complexity of nested loop dependent on the outer loop

What is the time complexity of this for loop nested in a while loop?

Time complexity with for loop nested in while loop

How to find the time complexity of nested for-loop

How to calculate time complexity of nested loop?

What is the time complexity of the following nested loop dependency?

What will be the time complexity of a for loop nested 2 times?

What is the time-complexity of this nested for loop?

what is the Time Complexity of a nested loop in a recursive function?

What is the time complexity for a nested loop in this case?

How to do this nested for loop time complexity?

Big-O Time Complexity of Nested Loop

What is the time complexity of this function? (nested for loop)

Time complexity of nested while loop inside nested for loop

Time Complexity Nested Loop Inner Loop + Outer Loop

How to figure out time complexity for a while loop nested in a for loop?

Time complexity of nested for loops where inner loop depends on outer loop

Big O time complexity for nested for loop (3 loop)

Big-O time complexity, nested for and while loop

3 Nested for loops where third loop is dependent on first time complexity