Algorithm RSearch(A,n,k)
//A is list of n distinct unsorted elements
//assuming index starts from 1 and ends at n
// k is element we want to search, assuming there is k at exactly one index
{
repeat
{
i= random(1,n); // pick random index from 1 to n with equal probability
if(A[i]==k)
return true;
}
until true;
}
答案 0 :(得分:0)
答案很大程度上取决于random(1,n)
功能。如果随机函数服从uniform distribution(意味着范围内每个数字的概率:[1,n]与其他数字相同)那么你可以考虑两种情况:
the complexity of best case is O(1)
the complexity of worst case is Omega(n)