vendredi 23 août 2019

The Randomized algorithm Parody

I was reading CLRS and I was stuck at question 5.1-2.

Describe an implementation of the procedure RANDOM(a,b) that only makes calls to RANDOM(0,1).What is the expected running time of your procedure, as a function of a and b?

The solution written here provides a complexity of O(lg(b-a)).

http://sites.math.rutgers.edu/~ajl213/CLRS/Ch5.pdf

I have too written an algorithm and I want some advice regarding it.

Random[a,b]
arr[]={a,....,b}
if(high-low <= 1)
 if(Random[0,1])
  new_arr[high]
  return
 else
  new_arr[low]
  return

if(Random[0,1])
 new_arr[a+b/2,.......,b]
else
 new_arr[a,...........,a+b/2-1]

My solution is Divide and Conquer based and that too with complexity O((b-a+1)).

Is my solution correct please tell?




Aucun commentaire:

Enregistrer un commentaire