我正在尝试推导确定性线性搜索算法的平均个案运行时间。该算法以A [1],A [2],A [3] ... A [n]的顺序搜索未排序数组A中的元素x。它在找到元素x或停止直到到达数组的末尾时停止。我搜索了wikipedia,给出的答案是(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)。
也许我是个笨蛋,但是如果数组大小为N,那么不是平均情况时间N/2? Nevermind ...不应该从我的iPhone发表评论...误读了q – Kevin
@kevin它没有重复的情况。但是当你有重复时,即使你发现第二次出现(为了计算平均复杂度),你将首先在你的搜索中获得。 – Zimbabao
@Kevin实际上没有多重拷贝情况的平均情况是(n + 1)/ 2。它可以这样得到:1 *(1/n)+ 2 *(1/n)+ 3 *(1/n)... + n *(1/n)。 – Brahadeesh