Meta Binary Search | One-Sided Binary Search
Last Updated :
21 Feb, 2023
Meta binary search (also called one-sided binary search by Steven Skiena in The Algorithm Design Manual on page 134) is a modified form of binary search that incrementally constructs the index of the target value in the array. Like normal binary search, meta binary search takes O(log n) time.
Meta Binary Search, also known as One-Sided Binary Search, is a variation of the binary search algorithm that is used to search an ordered list or array of elements. This algorithm is designed to reduce the number of comparisons needed to search the list for a given element.
The basic idea behind Meta Binary Search is to start with an initial interval of size n that includes the entire array. The algorithm then computes a middle element, as in binary search, and compares it to the target element. If the target element is found, the search terminates. If the middle element is greater than the target element, the algorithm sets the new interval to the left half of the previous interval, and if the middle element is less than the target element, the new interval is set to the right half of the previous interval. However, unlike binary search, Meta Binary Search does not perform a comparison for each iteration of the loop.
Instead, the algorithm uses a heuristic to determine the size of the next interval. It computes the difference between the value of the middle element and the value of the target element, and divides the difference by a predetermined constant, usually 2. This result is then used as the size of the new interval. The algorithm continues until it finds the target element or determines that it is not in the list.
The advantage of Meta Binary Search over binary search is that it can perform fewer comparisons in some cases, particularly when the target element is close to the beginning of the list. The disadvantage is that the algorithm may perform more comparisons than binary search in other cases, particularly when the target element is close to the end of the list. Therefore, Meta Binary Search is most effective when the list is ordered in a way that is consistent with the distribution of the target elements.
Here is the pseudocode for Meta Binary Search:
function meta_binary_search(A, target):
n = length(A)
interval_size = n
while interval_size > 0:
index = min(n - 1, interval_size / 2)
mid = A[index]
if mid == target:
return index
elif mid < target:
interval_size = (n - index) / 2
else:
interval_size = index / 2
return -1
Examples:
Input: [-10, -5, 4, 6, 8, 10, 11], key_to_search = 10
Output: 5
Input: [-2, 10, 100, 250, 32315], key_to_search = -2
Output: 0
The exact implementation varies, but the basic algorithm has two parts:
- Figure out how many bits are necessary to store the largest array index.
- Incrementally construct the index of the target value in the array by determining whether each bit in the index should be set to 1 or 0.
Approach:
- Store number of bits to represent the largest array index in variable lg.
- Use lg to start off the search in a for loop.
- If the element is found return pos.
- Otherwise, incrementally construct an index to reach the target value in the for loop.
- If element found return pos otherwise -1.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int bsearch (vector< int > A, int key_to_search)
{
int n = ( int )A.size();
int lg = log2(n-1)+1;
int pos = 0;
for ( int i = lg ; i >= 0; i--) {
if (A[pos] == key_to_search)
return pos;
int new_pos = pos | (1 << i);
if ((new_pos < n) && (A[new_pos] <= key_to_search))
pos = new_pos;
}
return ((A[pos] == key_to_search) ? pos : -1);
}
int main( void )
{
vector< int > A = { -2, 10, 100, 250, 32315 };
cout << bsearch (A, 10) << endl;
return 0;
}
|
Java
import java.util.Vector;
import com.google.common.math.BigIntegerMath;
import java.math.*;
class GFG {
static int bsearch(Vector<Integer> A, int key_to_search) {
int n = ( int ) A.size();
int lg = BigIntegerMath.log2(BigInteger.valueOf(n- 1 ),RoundingMode.UNNECESSARY) + 1 ;
int pos = 0 ;
for ( int i = lg - 1 ; i >= 0 ; i--) {
if (A.get(pos) == key_to_search) {
return pos;
}
int new_pos = pos | ( 1 << i);
if ((new_pos < n) && (A.get(new_pos) <= key_to_search)) {
pos = new_pos;
}
}
return ((A.get(pos) == key_to_search) ? pos : - 1 );
}
static public void main(String[] args) {
Vector<Integer> A = new Vector<Integer>();
int [] arr = {- 2 , 10 , 100 , 250 , 32315 };
for ( int i = 0 ; i < arr.length; i++) {
A.add(arr[i]);
}
System.out.println(bsearch(A, 10 ));
}
}
|
Python 3
import math
def bsearch(A, key_to_search):
n = len (A)
lg = int (math.log2(n - 1 )) + 1 ;
pos = 0
for i in range (lg - 1 , - 1 , - 1 ) :
if (A[pos] = = key_to_search):
return pos
new_pos = pos | ( 1 << i)
if ((new_pos < n) and
(A[new_pos] < = key_to_search)):
pos = new_pos
return (pos if (A[pos] = = key_to_search) else - 1 )
if __name__ = = "__main__" :
A = [ - 2 , 10 , 100 , 250 , 32315 ]
print ( bsearch(A, 10 ))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int bsearch(List< int > A, int key_to_search)
{
int n = ( int ) A.Count;
int lg = ( int )Math.Log(n-1, 2.0) + 1;
int pos = 0;
for ( int i = lg - 1; i >= 0; i--)
{
if (A[pos] == key_to_search)
{
return pos;
}
int new_pos = pos | (1 << i);
if ((new_pos < n) && (A[new_pos] <= key_to_search))
{
pos = new_pos;
}
}
return ((A[pos] == key_to_search) ? pos : -1);
}
static public void Main()
{
List< int > A = new List< int >();
int [] arr = {-2, 10, 100, 250, 32315};
for ( int i = 0; i < arr.Length; i++)
{
A.Add(arr[i]);
}
Console.WriteLine(bsearch(A, 10));
}
}
|
PHP
<?php
function bsearch( $A , $key_to_search , $n )
{
$lg = log( $n -1, 2) + 1;
$pos = 0;
for ( $i = $lg - 1; $i >= 0; $i --)
{
if ( $A [ $pos ] == $key_to_search )
return $pos ;
$new_pos = $pos | (1 << $i );
if (( $new_pos < $n ) &&
( $A [ $new_pos ] <= $key_to_search ))
$pos = $new_pos ;
}
return (( $A [ $pos ] == $key_to_search ) ?
$pos : -1);
}
$A = [ -2, 10, 100, 250, 32315 ];
$ans = bsearch( $A , 10, 5);
echo $ans ;
?>
|
Javascript
<script>
function bsearch(A, key_to_search)
{
let n = A.length;
let lg = parseInt(Math.log(n-1) / Math.log(2)) + 1;
let pos = 0;
for (let i = lg ; i >= 0; i--) {
if (A[pos] == key_to_search)
return pos;
let new_pos = pos | (1 << i);
if ((new_pos < n) && (A[new_pos] <= key_to_search))
pos = new_pos;
}
return ((A[pos] == key_to_search) ? pos : -1);
}
let A = [ -2, 10, 100, 250, 32315 ];
document.write(bsearch(A, 10));
</script>
|
Time Complexity: O(log n), where n is the size of the given array
Auxiliary Space: O(1) , as we are not using any extra space
Reference: https://www.quora.com/What-is-meta-binary-search
Please Login to comment...