【发布时间】:2011-07-04 18:36:25
【问题描述】:
我正在尝试推导确定性线性搜索算法的平均案例运行时间。该算法按照 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 吗?没关系...不应该在我的 iPhone 上发表评论...误读了 q
-
@kevin 没有重复的情况。但是,当您有重复项时,即使您发现第二次出现(以计算平均复杂度),您也会在搜索中获得第一名。
-
@Kevin 实际上没有多重副本的平均情况是 (n+1)/2。可以这样得到:1*(1/n)+ 2*(1/n)+3*(1/n)...+n*(1/n)。
标签: algorithm performance search combinatorics linear-search