I want to calculate the expected running time of randomized binary search of the following pseudo-code, where instead of considering the midpoint as the pivot, a random point is selected:
BinarySearch(x, A, start, end)
if(start == end)
if(A[end] == x)
return end
else
return -1
else
mid = RANDOM(start, end)
if(A[mid] == x)
return mid
else if(A[mid] > x)
return BinarySearch(x, A, start, mid-1)
else
return BinarySearch(x, A, mid+1, end)
I looked at this previous question, which has the following:
T(n) = sum ( T(r)*Pr(search space becomes r) ) + O(1) = sum ( T(r) )/n + O(1)
How is this obtained?
sum( T(r)*Pr(search space becomes r) )
And in the last line of calculation, how was this obtained?
T(n) = 1 + 1/2 + 1/3 + ... + 1/(n-1) = H(n-1) < H(n) = O(log n)
Aucun commentaire:
Enregistrer un commentaire