Uniform Binary Search
Last Updated :
06 Apr, 2023
Uniform Binary Search is an optimization of Binary Search algorithm when many searches are made on same array or many arrays of same size. In normal binary search, we do arithmetic operations to find the mid points. Here we precompute mid points and fills them in lookup table. The array look-up generally works faster than arithmetic done (addition and shift) to find the mid point.
Examples:
Input : array={1, 3, 5, 6, 7, 8, 9}, v=3
Output : Position of 3 in array = 2
Input :array={1, 3, 5, 6, 7, 8, 9}, v=7
Output :Position of 7 in array = 5
The algorithm is very similar to Binary Search algorithm, The only difference is a lookup table is created for an array and the lookup table is used to modify the index of the pointer in the array which makes the search faster . Instead of maintaining lower and upper bound the algorithm maintains an index and the index is modified using the lookup table.
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX_SIZE = 1000;
int lookup_table[MAX_SIZE];
void create_table( int n)
{
int pow = 1;
int co = 0;
do {
pow <<= 1;
lookup_table[co] = (n + ( pow >> 1)) / pow ;
} while (lookup_table[co++] != 0);
}
int binary( int arr[], int v)
{
int index = lookup_table[0] - 1;
int co = 0;
while (lookup_table[co] != 0) {
if (v == arr[index])
return index;
else if (v < arr[index])
index -= lookup_table[++co];
else
index += lookup_table[++co];
}
return index;
}
int main()
{
int arr[] = { 1, 3, 5, 6, 7, 8, 9 };
int n = sizeof (arr) / sizeof ( int );
create_table(n);
cout << "Position of 3 in array = "
<< binary(arr, 3) << endl;
return 0;
}
|
Java
class GFG
{
static int MAX_SIZE = 1000 ;
static int lookup_table[] = new int [MAX_SIZE];
static void create_table( int n)
{
int pow = 1 ;
int co = 0 ;
do
{
pow <<= 1 ;
lookup_table[co] = (n + (pow >> 1 )) / pow;
} while (lookup_table[co++] != 0 );
}
static int binary( int arr[], int v)
{
int index = lookup_table[ 0 ] - 1 ;
int co = 0 ;
while (lookup_table[co] != 0 )
{
if (v == arr[index])
return index;
else if (v < arr[index])
{
index -= lookup_table[++co];
}
else
{
index += lookup_table[++co];
}
}
return index ;
}
public static void main (String[] args)
{
int arr[] = { 1 , 3 , 5 , 6 , 7 , 8 , 9 };
int n = arr.length;
create_table(n);
System.out.println( "Position of 3 in array = " +
binary(arr, 3 )) ;
}
}
|
Python3
MAX_SIZE = 1000
lookup_table = [ 0 ] * MAX_SIZE
def create_table(n):
pow = 1
co = 0
while True :
pow << = 1
lookup_table[co] = (n + ( pow >> 1 )) / / pow
if lookup_table[co] = = 0 :
break
co + = 1
def binary(arr, v):
index = lookup_table[ 0 ] - 1
co = 0
while lookup_table[co] ! = 0 :
if v = = arr[index]:
return index
elif v < arr[index]:
co + = 1
index - = lookup_table[co]
else :
co + = 1
index + = lookup_table[co]
arr = [ 1 , 3 , 5 , 6 , 7 , 8 , 9 ]
n = len (arr)
create_table(n)
print ( "Position of 3 in array = " , binary(arr, 3 ))
|
C#
using System;
class GFG
{
static int MAX_SIZE = 1000;
static int []lookup_table = new int [MAX_SIZE];
static void create_table( int n)
{
int pow = 1;
int co = 0;
do
{
pow <<= 1;
lookup_table[co] = (n + (pow >> 1)) / pow;
} while (lookup_table[co++] != 0);
}
static int binary( int []arr, int v)
{
int index = lookup_table[0] - 1;
int co = 0;
while (lookup_table[co] != 0)
{
if (v == arr[index])
return index;
else if (v < arr[index])
{
index -= lookup_table[++co];
return index;
}
else
{
index += lookup_table[++co];
return index;
}
}
return index ;
}
public static void Main ()
{
int []arr = { 1, 3, 5, 6, 7, 8, 9 };
int n = arr.GetLength(0);
create_table(n);
Console.WriteLine( "Position of 3 in array = " +
binary(arr, 3)) ;
}
}
|
Javascript
<script>
let MAX_SIZE = 1000;
let lookup_table = new Array(MAX_SIZE);
lookup_table.fill(0);
function create_table(n)
{
let pow = 1;
let co = 0;
while ( true )
{
pow <<= 1;
lookup_table[co] = parseInt((n + (pow >> 1)) /
pow, 10);
if (lookup_table[co++] == 0)
{
break ;
}
}
}
function binary(arr, v)
{
let index = lookup_table[0] - 1;
let co = 0;
while (lookup_table[co] != 0)
{
if (v == arr[index])
return index;
else if (v < arr[index])
{
index -= lookup_table[++co];
return index;
}
else
{
index += lookup_table[++co];
return index;
}
}
return index ;
}
let arr = [ 1, 3, 5, 6, 7, 8, 9 ];
let n = arr.length;
create_table(n);
document.write( "Position of 3 in array = " +
binary(arr, 3));
</script>
|
Output:
Position of 3 in array = 1
Time Complexity : O(log n).
Auxiliary Space Complexity : O(log n)
References : https://en.wikipedia.org/wiki/Uniform_binary_search
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...