Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Big-O Nested While Loop

i <-- 1
while(i < n)
   j <--1
   while(j < i)
      j <-- j * 2
   i <-- i + 1
done

My shot at this would be O(log n) for the inner loop. And I'm guessing the outer loop is O(n), for an overall complexity of O(n log n). Confirm?

like image 421
user3818430 Avatar asked Jan 10 '23 06:01

user3818430


1 Answers

You may proceed formally, step by step, using Sigma notation to obtain the exact number of iterations - Look at Discrete Loops and Worst Case Performance paper (page 10).

enter image description here

The result has been empirically verified.

like image 131
Mohamed Ennahdi El Idrissi Avatar answered Jan 22 '23 05:01

Mohamed Ennahdi El Idrissi