admin管理员组文章数量:1435859
Maybe a bit of a silly question but the run time analysis for this code is confusing me.
int i, j;
int k = 0;
for(i = 1; i<= n; i*= 2){
for (j = 1; j <= i; j++){
k += (i+j);
}
}
I understand that the outer loops runtime would be log(base2)n and that the overall value of the runtime is the runtime of the outer loop * the runtime of the inner loop. I'm just a little confused about the inner loop, I know the inner loop is iterating based on the value of i
.
So would the inner loops runtime also be log(base2)n and then the overall runtime would be (log(base2)n)^2?
Somehow I don't feel like that's right.
Maybe a bit of a silly question but the run time analysis for this code is confusing me.
int i, j;
int k = 0;
for(i = 1; i<= n; i*= 2){
for (j = 1; j <= i; j++){
k += (i+j);
}
}
I understand that the outer loops runtime would be log(base2)n and that the overall value of the runtime is the runtime of the outer loop * the runtime of the inner loop. I'm just a little confused about the inner loop, I know the inner loop is iterating based on the value of i
.
So would the inner loops runtime also be log(base2)n and then the overall runtime would be (log(base2)n)^2?
Somehow I don't feel like that's right.
3 Answers
Reset to default 1The complexity in this case is determined by the number of times the inner loop will execute, multiplied by the complexity of body of the inner loop.
Regarding the number of times it will execute:
The outer loop will have the values 1,2,4,8,...,n.
In each of the outer loop iterations the inner loop will execute times according to this value.
Therefore altogether the inner loop will execute:
1+2+4+8+...+n.
This is a geometric series with a (first element) of 1, r (factor) of 2, and containing log(n) elements.
The formula for the sum of m elements of a geometric series is (r != 1):
Sm = a * (1 - r ^ (m+1)) / (1 - r)
If we assign a=1, r=2, m=log(n) we get:
1 * (1 - 2 ^ (logn+1)) / (1-2) =
1 * (1 - 2 * 2 ^ logn) / (1-2) =
1 * (1 - 2n)) / (-1) =
1 * (2n - 1) =
2*n - 1
=>
O(n)
In this specific case you can also see it as the value of a binary value of logn bits, all set to 1, which is again 2*n - 1 => O(n).
Regarding the body of the inner loop:
It contains a constant amount of calculations and so it is O(1).
=> Therefore the overall complexity is O(n) * O(1) = O(n).
Note:
This assumes that we are dealing with limited size integers (e.g. 64 bit), which is what types like int
that you used usually are. If you want to include the analysis for arbitrary large integers up to n, representing and doing operations like addition (which you used) on them becomes O(logn) instead of O(1).
The code has two loops:
Outer Loop:
- Starts with i=1 and multiplies i by 2 each time
- Stops when i becomes larger than n
- So, it runs log2(n) times (since multiplying by 2 repeatedly is the same as dividing n by 2 in reverse).
Inner Loop:
- Runs from j=1 to j=i
- The number of times it runs depends on the value of i
- For example, if i=4, the inner loop runs 4 times
So
- When i=1, the inner loop runs 1 time.
- When i=2, the inner loop runs 2 times.
- When i=4, the inner loop runs 4 times.
- When i=8, the inner loop runs 8 times.
This continues until i reaches n.
total number of iterations of the inner loop is: 1+2+4+8+⋯+n
This means the inner loop runs
本文标签: algorithmRun Time Anaysis of Nested LoopStack Overflow
版权声明:本文标题:algorithm - Run Time Anaysis of Nested Loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745667953a2669394.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
int
type allows for arbitrary large integers (not just 64-bit). – trincot Commented Nov 16, 2024 at 7:58