Reputation: 471
Here is a nested for loop. I work out the time complexity for it is nlg(n).
int sum = 0;
for(int k = 1; k < n; k*=2){
for(int i = 1; i < n; i++){
sum++;
}
}
My thoughts are as follows.
k
will take values of 1, 2, 4, 8, ... Thus it will take lg(n) iteration.i
will take n iteration.Hence, the overall operations taken will be nlg(n).
Am I right?
Upvotes: 0
Views: 148
Reputation: 2977
Yes, the order of growth you suggested is correct. You can show it like the following:
Upvotes: 0