这是另一种方法。我在其中一些计算中可能是错误的,所以请纠正我。
我们可以写
T(n) = 2 T(n/2) + c for all n > 1, where c is some constant. And
T(n) = 1 when n = 1
So T(n) = 2 T(n/2) + c, now start substituting T(n/2) and move one
=> T(n) = 2 [ 2 T(n/4) + c ] + c
=> T(n) = 2^2T(n/4) + 2c
Now substitute t(n/4) as well
=> T(n) = 2^2[2 T(n/8) + c] + 2c
=> T(n) = 2^3T(n/8) + 3c
Now assume that if we keep dividing like this, at some point we will reach 1 i.e., when n/2^k = 1, then T(1) = 1
=> T(n) = 2^kT(n/2^k) + kc
Now since we know that n/2^k = 1
=> k = log n (I am representing log as base 2)
Therefore substitute k value in above T(n) equation to get
=> T(n) = 2^(log n) T(1) + c log n
=> T(n) = n T(1) + c log n (Check log rule on how we got n for first coefficient)
=> T(n) = n + c log n (since T(1) = 1)
因此 T(n) = O(n) 因为 n 在增长率上占 log n 的主导地位。