我试图得出确定性线性搜索算法的平均案例运行时间。该算法按照 A[1]、A[2]、A[3]...A[n] 的顺序搜索未排序数组 A 中的元素 x。它在找到元素 x 时停止或继续直到到达数组的末尾。我在维基百科上搜索,给出的答案是 (n+1)/(k+1) 其中 k 是 x 出现在数组中的次数。我以另一种方式接近,得到了不同的答案。谁能给我正确的证据,并让我知道我的方法有什么问题?
E(T)= 1*P(1) + 2*P(2) + 3*P(3) ....+ n*P(n) where P(i) is the probability that
the algorithm runs for 'i' time (i.e. compares 'i' elements).
P(i)= (n-i)C(k-1) * (n-k)! / n!
Here, (n-i)C(k-1) is (n-i) Choose (k-1). As the algorithm has reached the ith
step, the rest of k-1 x's must be in the last n-i elements. Hence (n-i)C(k-i).
(n-k)! is the total number of ways of arranging the rest non x numbers, and n!
is the total number of ways of arranging the n elements in the array.
我没有得到 (n+1)/(k+1) 的简化。