Yes you are right.
A simple way to calculate is
for(int i=0; i<n;i++){ // n times for(int j=0; j<n;j++){ // n times } }
This example is a simple nested loop. Here, the Big-O of each loop is O (n), and it is nested so typically O (n * n), which is O (n ^ 2) the actual Big-O. And in your case -
for (int i = n; i > 0; i = i / 2){ // log(n) for (int j = n; j > 0; j = j / 2){ // log(n) for (int k = n; k > 0; k = k / 2){ // log(n) count++; } } }
What is in the nested loop, where each Big-O loop is O(log(n)) , so all together will be O(log(n)^3)
source share