Randomized Binary Search Algorithm
Last Updated :
25 Jan, 2022
We are given a sorted array A[] of n elements. We need to find if x is present in A or not.In binary search we always used middle element, here we will randomly pick one element in given range.
In Binary Search we had
middle = (start + end)/2
In Randomized binary search we do following
Generate a random number t
Since range of number in which we want a random
number is [start, end]
Hence we do, t = t % (end-start+1)
Then, t = start + t;
Hence t is a random number between start and end
It is a Las Vegas randomized algorithm as it always finds the correct result.
Expected Time complexity of Randomized Binary Search Algorithm
For n elements let say expected time required be T(n), After we choose one random pivot, array size reduces to say k. Since pivot is chosen with equal probability for all possible pivots, hence p = 1/n.
T(n) is sum of time of all possible sizes after choosing pivot multiplied by probability of choosing that pivot plus time take to generate random pivot index.Hence
T(n) = p*T(1) + p*T(2) + ..... + p*T(n) + 1
putting p = 1/n
T(n) = ( T(1) + T(2) + ..... + T(n) ) / n + 1
n*T(n) = T(1) + T(2) + .... + T(n) + n .... eq(1)
Similarly for n-1
(n-1)*T(n-1) = T(1) + T(2) + ..... + T(n-1) + n-1 .... eq(2)
Subtract eq(1) - eq(2)
n*T(n) - (n-1)*T(n-1) = T(n) + 1
(n-1)*T(n) - (n-1)*T(n-1) = 1
(n-1)*T(n) = (n-1)*T(n-1) + 1
T(n) = 1/(n-1) + T(n-1)
T(n) = 1/(n-1) + 1/(n-2) + T(n-2)
T(n) = 1/(n-1) + 1/(n-2) + 1/(n-3) + T(n-3)
Similarly,
T(n) = 1 + 1/2 + 1/3 + ... + 1/(n-1)
Hence T(n) is equal to (n-1)th Harmonic number,
n-th harmonic number is O(log n)
Hence T(n) is O(log n)
Recursive implementation of Randomized Binary Search
C++
#include <iostream>
#include <ctime>
using namespace std;
int getRandom( int x, int y)
{
srand ( time (NULL));
return (x + rand () % (y-x+1));
}
int randomizedBinarySearch( int arr[], int l,
int r, int x)
{
if (r >= l)
{
int mid = getRandom(l, r);
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return randomizedBinarySearch(arr, l,
mid-1, x);
return randomizedBinarySearch(arr, mid+1,
r, x);
}
return -1;
}
int main( void )
{
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof (arr)/ sizeof (arr[0]);
int x = 10;
int result = randomizedBinarySearch(arr, 0, n-1, x);
(result == -1)? printf ( "Element is not present in array" )
: printf ( "Element is present at index %d" , result);
return 0;
}
|
Java
public class RandomizedBinarySearch
{
public static int getRandom( int x, int y)
{
return (x + ( int )(Math.random() % (y-x+ 1 )));
}
public static int randomizedBinarySearch( int arr[],
int low, int high, int key)
{
if (high >= low)
{
int mid = getRandom(low, high);
if (arr[mid] == key)
return mid;
if (arr[mid] > key)
return randomizedBinarySearch(arr, low, mid- 1 , key);
return randomizedBinarySearch(arr, mid+ 1 , high, key);
}
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 4 , 10 , 40 };
int n = arr.length;
int key = 10 ;
int result = randomizedBinarySearch(arr, 0 , n- 1 , key);
System.out.println((result == - 1 )? "Element is not present in array" :
"Element is present at index " + result);
}
}
|
Python3
import random
def getRandom(x,y):
tmp = (x + random.randint( 0 , 100000 ) % (y - x + 1 ))
return tmp
def randomizedBinarySearch(arr,l,r,x) :
if r> = l:
mid = getRandom(l,r)
if arr[mid] = = x:
return mid
if arr[mid]>x:
return randomizedBinarySearch(arr, l, mid - 1 , x)
return randomizedBinarySearch(arr, mid + 1 ,r, x)
return - 1
if __name__ = = '__main__' :
arr = [ 2 , 3 , 4 , 10 , 40 ]
n = len (arr)
x = 10
result = randomizedBinarySearch(arr, 0 , n - 1 , x)
if result = = - 1 :
print ( 'Element is not present in array' )
else :
print ( 'Element is present at index ' , result)
|
C#
using System;
class RandomizedBinarySearch
{
public static int getRandom( int x, int y)
{
Random r = new Random();
return (x + ( int )(r.Next() % (y - x + 1)));
}
public static int randomizedBinarySearch( int []arr,
int low, int high, int key)
{
if (high >= low)
{
int mid = getRandom(low, high);
if (arr[mid] == key)
return mid;
if (arr[mid] > key)
return randomizedBinarySearch(arr, low, mid - 1, key);
return randomizedBinarySearch(arr, mid + 1, high, key);
}
return -1;
}
public static void Main(String[] args)
{
int []arr = {2, 3, 4, 10, 40};
int n = arr.Length;
int key = 10;
int result = randomizedBinarySearch(arr, 0, n - 1, key);
Console.WriteLine((result == -1)? "Element is not present in array" :
"Element is present at index " + result);
}
}
|
Javascript
<script>
function getRandom(x, y) {
return (x + Math.floor(Math.random() % (y - x + 1)));
}
function randomizedBinarySearch(arr, l, r, x)
{
if (r >= l)
{
let mid = getRandom(l, r);
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return randomizedBinarySearch(arr, l,
mid - 1, x);
return randomizedBinarySearch(arr, mid + 1,
r, x);
}
return -1;
}
let arr = [2, 3, 4, 10, 40];
let n = arr.length;
let x = 10;
let result = randomizedBinarySearch(arr, 0, n - 1, x);
(result == -1) ? document.write( "Element is not present in array" )
: document.write( "Element is present at index " + result);
</script>
|
Output:
Element is present at index 3
Iterative implementation of Randomized Binary Search
C++
#include <iostream>
#include <ctime>
using namespace std;
int getRandom( int x, int y)
{
srand ( time (NULL));
return (x + rand ()%(y-x+1));
}
int randomizedBinarySearch( int arr[], int l,
int r, int x)
{
while (l <= r)
{
int m = getRandom(l, r);
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
int main( void )
{
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof (arr)/ sizeof (arr[0]);
int x = 10;
int result = randomizedBinarySearch(arr, 0, n-1, x);
(result == -1)? printf ( "Element is not present in array" )
: printf ( "Element is present at index %d" , result);
return 0;
}
|
Java
class GFG
{
static int getRandom( int x, int y)
{
return ( int ) (x + Math.random() * 10 % (y - x + 1 ));
}
static int randomizedBinarySearch( int arr[], int l,
int r, int x)
{
while (l <= r)
{
int m = getRandom(l, r);
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1 ;
else
r = m - 1 ;
}
return - 1 ;
}
public static void main(String []args)
{
int arr[] = { 2 , 3 , 4 , 10 , 40 };
int n = arr.length;
int x = 10 ;
int result = randomizedBinarySearch(arr, 0 , n - 1 , x);
if (result == - 1 )
System.out.printf( "Element is not present in array" );
else
System.out.printf( "Element is present at index %d" , result);
}
}
|
Python3
from random import randint
def getRandom(x, y):
return randint(x,y)
def randomizedBinarySearch(arr, l, r, x):
while (l < = r):
m = getRandom(l, r)
if (arr[m] = = x):
return m
if (arr[m] < x):
l = m + 1
else :
r = m - 1
return - 1
arr = [ 2 , 3 , 4 , 10 , 40 ]
n = len (arr)
x = 10
result = randomizedBinarySearch(arr, 0 , n - 1 , x)
if result = = 1 :
print ( "Element is not present in array" )
else :
print ( "Element is present at index" , result)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int getRandom( int x, int y)
{
return ( int ) (x + new Random(10).Next(1) * 10 % (y - x + 1));
}
static int randomizedBinarySearch( int []arr, int l,
int r, int x)
{
while (l <= r)
{
int m = getRandom(l, r);
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
public static void Main(String []args)
{
int []arr = {2, 3, 4, 10, 40};
int n = arr.Length;
int x = 10;
int result = randomizedBinarySearch(arr, 0, n - 1, x);
if (result == -1)
Console.Write( "Element is not present in array" );
else
Console.Write( "Element is present at index {0}" , result);
}
}
|
Javascript
<script>
function getRandom(x,y)
{
return Math.floor(x + Math.floor(Math.random() * 10) %
(y - x + 1));
}
function randomizedBinarySearch(arr,l,r,x)
{
while (l <= r)
{
let m = getRandom(l, r);
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
let arr = [ 2, 3, 4, 10, 40 ];
let n = arr.length;
let x = 10;
let result = randomizedBinarySearch(arr, 0, n - 1, x);
if (result == -1)
document.write( "Element is not present in array" );
else
document.write( "Element is present at index " ,
result);
</script>
|
Output:
Element is present at index 3
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...