时间复杂度为循环 [英] Time Complexity for loop
本文介绍了时间复杂度为循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图找到一个for循环的时间复杂度。下面是循环的细节。
I was trying to find the time complexity for a for loop. below is loop detail.
for(int i=N;i>1;i=i/2)
{
for(int k=0;k<i;k++){
sum++;
}
}
下面是任何发现的问题。请纠正我,如果我要worng。
Below is any find for the problem. Please correct me if i am going worng.
内环将exceute N + N / 2 + N / 4 + N / 8 ......
Inner loop will be exceute N+N/2+N/4+N/8....
让 TN = ^ AR(N-1)
。因此,将 Tn的= 1
, A = N
和 R = 1 /
1=N(1/2)^(n-1)
因此,
1/2N=(1/2)^n
所以,内环之和为GP。 SN = A(1-R ^ N)/(1-R)
更换 A = N,R = 1 /
,我们得到
Sn=N(1-(1/2N))/(1-1/2)
因此, SN = 2N-1
我不知道,如果是复杂 N
。
I am not sure if complexity is N
.
请帮忙
感谢。
推荐答案
下面是正规途径(西格玛符号)来推断成长关系到你的算法的顺序(与确证实验,用C的MinGW2.95编译器)。
Below is the formal way (Sigma Notation) to infer the order of growth related to your algorithm (corroborated with experiments, using C's MinGW2.95 compiler).
这篇关于时间复杂度为循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文