Algorithm 递归函数的数字执行

Algorithm 递归函数的数字执行,algorithm,recursion,complexity-theory,divide-and-conquer,Algorithm,Recursion,Complexity Theory,Divide And Conquer,我有这个递归算法,T(n)是p(a,b)被执行的次数,n:=b-a int foo[] = ... // array of big enough size function P(int a, int b) if (a+1 < b) { int h = floor((a+b)/2) if foo[h] >= 0 then P(a, h) if foo[h] <= 0 then P(h, b) } end function

我有这个递归算法,T(n)是p(a,b)被执行的次数,n:=b-a

int foo[] = ... // array of big enough size
function P(int a, int b) 
    if (a+1 < b) {
       int h = floor((a+b)/2)
       if foo[h] >= 0 then P(a, h)
       if foo[h] <= 0 then P(h, b)
    }
end function
intfoo[]=…//足够大的数组
函数P(整数a,整数b)
如果(a+1=0,那么P(a,h)

如果foo[h]每次
a
b
之间的距离(作为函数的输入)减半,时间复杂度是
Theta(log(b-a))
h
a
b
之间的中点。因此,距离
[a,h]
[h,b]/code>都有长度
ceil(n/2)
(假设
foo
总是选择较大的范围)。有了这些知识,你应该能够计算计时。你是如何得出这样的结论的:他们的长度ceil(n/2)忽略舍入,我们可以计算
h-a=(a+b)/2-a=b/2-a/2=(b-a)/2=n/2
。同样适用于
b-h