I am studying algorithms complexity, and I have a question about the difference between the following two algorithms:
Algorithm #1:
sum = 0
i = 1
while (i < n) {
for j = 1 to i {
sum = sum + 1
}
i = i*2;
}
return sum
Algorithm #2:
sum = 0
i = 1
while (i < n) {
for j = 1 to n {
sum = sum + 1
}
i = i*2;
}
return sum
The only difference is the 'for' loop, but what is the difference between the time complexity of these algorithms ? When do I have to multiply or add the complexity of nested loops?
答案 0 :(得分:3)
Let say for simplisity that n is a power of 2
i.e. 2^k
. Then it is obvious that outer loop will proceed k times and on each step inner loop will proceed:
1 to 1 i.e. 2^0
1 to 2 i.e. 2^1
1 to 4 i.e. 2^2
...
1 to 2^k
So we just need to find the sum 2^0 + 2^1 + ... + 2^k
It is knows as 2^(k+1) - 1 = 2^k * 2 - 1 = 2*n + 1
So omitting constants we get O(n)
Second one is simple. Outer loop is log(n)
, inner n
so you get O(n*log(n))
.