【发布时间】:2014-09-29 15:46:56
【问题描述】:
对于下面的mystery(n) 函数的伪代码,找到其渐近最坏情况运行时间f(n) 的严格上限和下限。也就是说,找到g(n) 使得f(n) ∈ Θ(g(n))。 (假设n为正整数)
Mystery (n ){
c ←1 | (constant)
for i ←1 to n | n
do for j ←i to n | j
do for k ← n down to n/2 | n/2
do c ← c + 1 | (constant)
print c | (constant)
}
总时间:(n/2)nj(不确定)
侧面的时间标签是我迄今为止发现的。对于这个问题,最好和最坏情况的运行时间似乎没有区别?此外,我怎样才能找到这种方法的严格上限和下限?任何建议都会很棒。或者我可以阅读的资源,因为我的教科书在这个主题上非常模糊。
【问题讨论】:
-
尝试将其写为仅外循环上的操作总和
(n^2/2 + (n-1)*n/2 + ...)
标签: algorithm big-o complexity-theory asymptotic-complexity