Uniform Binary Search
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++
// C++ implemenatation of above approach #include <bits/stdc++.h> using namespace std; const int MAX_SIZE = 1000; // lookup table int lookup_table[MAX_SIZE]; // create the lookup table // for an array of length n void create_table(int n) { // power and count variable int pow = 1; int co = 0; do { // multiply by 2 pow <<= 1; // initialize the lookup table lookup_table[co] = (n + (pow >> 1)) / pow; } while (lookup_table[co++] != 0); } // binary search int binary(int arr[], int v) { // mid point of the array int index = lookup_table[0] - 1; // count int co = 0; while (lookup_table[co] != 0) { // if the value is found if (v == arr[index]) return index; // if value is less than the mid value else if (v < arr[index]) index -= lookup_table[++co]; // if value is greater than the mid value else index += lookup_table[++co]; } } // main function int main() { int arr[] = { 1, 3, 5, 6, 7, 8, 9 }; int n = sizeof(arr) / sizeof(int); // create the lookup table create_table(n); // print the position of the array cout << "Position of 3 in array = " << binary(arr, 3) << endl; return 0; } |
Java
// Java implemenatation of above approach class GFG { static int MAX_SIZE = 1000; // lookup table static int lookup_table[] = new int[MAX_SIZE]; // create the lookup table // for an array of length n static void create_table(int n) { // power and count variable int pow = 1; int co = 0; do { // multiply by 2 pow <<= 1; // initialize the lookup table lookup_table[co] = (n + (pow >> 1)) / pow; } while (lookup_table[co++] != 0); } // binary search static int binary(int arr[], int v) { // mid point of the array int index = lookup_table[0] - 1; // count int co = 0; while (lookup_table[co] != 0) { // if the value is found if (v == arr[index]) return index; // if value is less than the mid value else if (v < arr[index]) { index -= lookup_table[++co]; return index; } // if value is greater than the mid value else { index += lookup_table[++co]; return index; } } return index ; } // Driver code public static void main (String[] args) { int arr[] = { 1, 3, 5, 6, 7, 8, 9 }; int n = arr.length; // create the lookup table create_table(n); // print the position of the array System.out.println( "Position of 3 in array = " + binary(arr, 3)) ; } } // This code is contributed by Ryuga |
C#
// C# implemenatation of above approach using System; class GFG { static int MAX_SIZE = 1000; // lookup table static int []lookup_table = new int[MAX_SIZE]; // create the lookup table // for an array of length n static void create_table(int n) { // power and count variable int pow = 1; int co = 0; do { // multiply by 2 pow <<= 1; // initialize the lookup table lookup_table[co] = (n + (pow >> 1)) / pow; } while (lookup_table[co++] != 0); } // binary search static int binary(int []arr, int v) { // mid point of the array int index = lookup_table[0] - 1; // count int co = 0; while (lookup_table[co] != 0) { // if the value is found if (v == arr[index]) return index; // if value is less than the mid value else if (v < arr[index]) { index -= lookup_table[++co]; return index; } // if value is greater than the mid value else { index += lookup_table[++co]; return index; } } return index ; } // Driver code public static void Main () { int []arr = { 1, 3, 5, 6, 7, 8, 9 }; int n = arr.GetLength(0); // create the lookup table create_table(n); // print the position of the array Console.WriteLine( "Position of 3 in array = " + binary(arr, 3)) ; } } /* This code contributed by PrinciRaj1992 */ |
Position of 3 in array = 1
References : https://en.wikipedia.org/wiki/Uniform_binary_search
Recommended Posts:
- Uniform-Cost Search (Dijkstra for large Graphs)
- Meta Binary Search | One-Sided Binary Search
- Why is Binary Search preferred over Ternary Search?
- Interpolation search vs Binary search
- Linear Search vs Binary Search
- Binary Search
- Binary Search in PHP
- Variants of Binary Search
- Binary Search a String
- The Ubiquitous Binary Search | Set 1
- Binary Search in Java
- Binary Search using pthread
- Binary Search In JavaScript
- Binary Search (bisect) in Python
- A Problem in Many Binary Search Implementations
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



