Time complexity nested loop

M0rty

I'm having a hard time understanding algorithm analysis, especially the following example:

for (int i = 1; i < n^3; i = 2*i){
  for (int j = 6*n; j > 0; j = j-2){
  }
}

So my understanding of this is that the outer loop is O(nlogn) as i is multiplied by a constant amount, I'm unsure if the n^3 makes any difference or not.

The inner loop confuses me the most though. I think it's O(n) as j is decremented by a constant amount but I'm uncertain whether the 6*n and the j > 0 have any influence.

If i'm correct then this entire algorithm would be O(nlogn)? Uncertain how to express the time complexity T(n) of this though.

Any insights would be hugely appreciated.

Nir Alfasi

The outer loop is not O(nlogn) - since i is multiplied in 2 every loop, it goes: 2, 4, 8, 16, 32... this means that it will take a log(n^3) steps for i to reach n^3 (the base of the log is 2).

More formal: O(log(n^3)) = O(3logn) = O(logn)

In the inner loop j is initialized to 6*n and goes down in steps of 2. This means that it'll take 6n/2 steps for j to reach zero.

More formal: O(6n/2) = O(3n) = O(n)

So the complexity of the code section is O(n) * O(logn) = O(nlogn)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Time complexity of nested for loop

Time complexity for a nested for loop

Time Complexity - nested for loop

Time complexity on 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