确定最坏情况算法的时间复杂度
这两种算法是否具有相同的 θ(n^2) θ 特性?
int sum = 0;
for (int i = 0; i < n; i++ )
for (int j = 0; j < n * n; j++ )
sum++;
int sum = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < i; j++)
sum++;
如果不是,那么这是否意味着该表征不是 θ(n^3)?
int sum = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < i * i; j++ )
for ( int k = 0; k < j; k++ )
sum++;
Do the two algorithms have the same theta characterization of Θ(n^2)?
int sum = 0;
for (int i = 0; i < n; i++ )
for (int j = 0; j < n * n; j++ )
sum++;
int sum = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < i; j++)
sum++;
If not then does this mean that this characterization is not Θ(n^3)?
int sum = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < i * i; j++ )
for ( int k = 0; k < j; k++ )
sum++;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@Dan,对于第一个,您的意思是
j << n * n
而不是j
n?如果是的话,第一个的时间复杂度就是 θ(n^3),不是吗?
如果你的意思是
j < n
,那么我相信前两个都是 θ(n^2):第一个需要 n^2 步,第二个需要 1 + 2 + ... + n = n(n+1 )/2 即 θ(n^2)。我认为第三个是 θ(n^4),但很难证明。绝对是 O(n^4)。
@Dan, For the first one did you really mean
j < n * n
rather thanj < n
? If so, the time complexity of the first one is Θ(n^3), isn't it?If you meant
j < n
, then I believe the first two are both Θ(n^2): The first one takes n^2 steps, and the second one takes 1 + 2 + ... + n = n(n+1)/2 which is Θ(n^2).I'm thinking the 3rd one is Θ(n^4), but it's harder to prove. Definitely O(n^4).