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.
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 element is found return pos
- Otherwise incrementally construct 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++
// C++ implementation of above approach #include <iostream> #include <cmath> #include <vector> using namespace std; // Function to show the working of Meta binary search int bsearch (vector< int > A, int key_to_search) { int n = ( int )A.size(); // Set number of bits to represent largest array index int lg = log2(n-1)+1; //while ((1 << lg) < n - 1) //lg += 1; int pos = 0; for ( int i = lg ; i >= 0; i--) { if (A[pos] == key_to_search) return pos; // Incrementally construct the // index of the target value int new_pos = pos | (1 << i); // find the element in one // direction and update position if ((new_pos < n) && (A[new_pos] <= key_to_search)) pos = new_pos; } // if element found return pos otherwise -1 return ((A[pos] == key_to_search) ? pos : -1); } // Driver code int main( void ) { vector< int > A = { -2, 10, 100, 250, 32315 }; cout << bsearch (A, 10) << endl; return 0; } // This implementation was improved by Tanin |
Java
//Java implementation of above approach import java.util.Vector; import com.google.common.math.BigIntegerMath; import java.math.*; class GFG { // Function to show the working of Meta binary search static int bsearch(Vector<Integer> A, int key_to_search) { int n = ( int ) A.size(); // Set number of bits to represent largest array index int lg = BigIntegerMath.log2(BigInteger.valueOf(n- 1 ),RoundingMode.UNNECESSARY) + 1 ; //while ((1 << lg) < n - 1) { // lg += 1; //} int pos = 0 ; for ( int i = lg - 1 ; i >= 0 ; i--) { if (A.get(pos) == key_to_search) { return pos; } // Incrementally construct the // index of the target value int new_pos = pos | ( 1 << i); // find the element in one // direction and update position if ((new_pos < n) && (A.get(new_pos) <= key_to_search)) { pos = new_pos; } } // if element found return pos otherwise -1 return ((A.get(pos) == key_to_search) ? pos : - 1 ); } // Driver code 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 )); } } // This code is contributed by 29AjayKumar // This implementation was improved by Tanin |
Python 3
# Python 3 implementation of # above approach # Function to show the working # of Meta binary search import math def bsearch(A, key_to_search): n = len (A) # Set number of bits to represent lg = int (math.log2(n - 1 )) + 1 ; # largest array index #while ((1 << lg) < n - 1): #lg += 1 pos = 0 for i in range (lg - 1 , - 1 , - 1 ) : if (A[pos] = = key_to_search): return pos # Incrementally construct the # index of the target value new_pos = pos | ( 1 << i) # find the element in one # direction and update position if ((new_pos < n) and (A[new_pos] < = key_to_search)): pos = new_pos # if element found return # pos otherwise -1 return (pos if (A[pos] = = key_to_search) else - 1 ) # Driver code if __name__ = = "__main__" : A = [ - 2 , 10 , 100 , 250 , 32315 ] print ( bsearch(A, 10 )) # This implementation was improved by Tanin # This code is contributed # by ChitraNayal |
C#
//C# implementation of above approach using System; using System.Collections.Generic; class GFG { // Function to show the working of Meta binary search static int bsearch(List< int > A, int key_to_search) { int n = ( int ) A.Count; //int lg = 0; // Set number of bits to represent largest array index int lg = ( int )Math.Log(n-1, 2.0) + 1; // This is redundant and will cause error //while ((1 << lg) < n - 1) //{ // lg += 1; //} int pos = 0; for ( int i = lg - 1; i >= 0; i--) { if (A[pos] == key_to_search) { return pos; } // Incrementally construct the // index of the target value int new_pos = pos | (1 << i); // find the element in one // direction and update position if ((new_pos < n) && (A[new_pos] <= key_to_search)) { pos = new_pos; } } // if element found return pos otherwise -1 return ((A[pos] == key_to_search) ? pos : -1); } // Driver code 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)); } } // This code is contributed by Rajput-Ji // This implementation was improved by Tanin |
PHP
<?php // PHP implementation of above approach // Function to show the working of // Meta binary search function bsearch( $A , $key_to_search , $n ) { // Set number of bits to represent $lg = log( $n -1, 2) + 1; // largest array index // This is redundant and will cause error for some case //while ((1 << $lg) < $n - 1) //$lg += 1; $pos = 0; for ( $i = $lg - 1; $i >= 0; $i --) { if ( $A [ $pos ] == $key_to_search ) return $pos ; // Incrementally construct the // index of the target value $new_pos = $pos | (1 << $i ); // find the element in one // direction and update $position if (( $new_pos < $n ) && ( $A [ $new_pos ] <= $key_to_search )) $pos = $new_pos ; } // if element found return $pos // otherwise -1 return (( $A [ $pos ] == $key_to_search ) ? $pos : -1); } // Driver code $A = [ -2, 10, 100, 250, 32315 ]; $ans = bsearch( $A , 10, 5); echo $ans ; // This code is contributed by AdeshSingh1 // This implementation was improved by Tanin ?> |
1
Reference: https://www.quora.com/What-is-meta-binary-search
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.