Bitwise Binary Search algorithm
Last Updated :
17 May, 2022
Pre-requisite: Binary Search
Bitwise Binary Search algorithm is a modified version of Binary Search based on the following idea:
Every number can be represented as a sum of the powers of the number 2.
Examples:
- 76 = 64 + 8 + 4
- 10 = 8 + 2
- 7 = 4 + 2 + 1
Approach:
- Compute the first power of 2 that is greater or equal then the size of the array.
- Initialize an index as 0.
- Loop while the computed power is greater than 0 and each time divide it by 2.
- Each time the element at position [index + power] <= target we add to the index variable the respective power value. (Build the sum)
- After the for loops check if the element at position [index] == target. If so the target element is present in the array, else not.
- (no division needed only addition and bitwise shifting)
C++
#include <iostream>
using namespace std;
int binary_search( int * arr, int size, int target)
{
int index, power;
for (power = 1; power < size; power <<= 1)
;
for (index = 0; power; power >>= 1) {
if (index + power < size
&& arr[index + power] <= target)
index += power;
}
if (arr[index] == target)
return index;
return -1;
}
int main()
{
int arr[5] = { 1, 3, 5, 7, 8 };
int size = 5;
int x = 3;
int answer = binary_search(arr, size, x);
if (answer == -1)
cout << "Element not found" ;
else
cout << "Element found at position " << answer;
}
|
Java
import java.util.*;
class GFG {
static int binary_search( int [] arr, int size,
int target)
{
int index, power;
for (power = 1 ; power < size; power <<= 1 )
;
for (index = 0 ; power > 0 ; power >>= 1 ) {
if (index + power < size
&& arr[index + power] <= target)
index += power;
}
if (arr[index] == target)
return index;
return - 1 ;
}
public static void main(String[] args)
{
int [] arr = { 1 , 3 , 5 , 7 , 8 };
int size = 5 ;
int x = 3 ;
int answer = binary_search(arr, size, x);
if (answer == - 1 )
System.out.print( "Element not found" );
else
System.out.print( "Element found at position "
+ answer);
}
}
|
Python3
def binary_search(arr, size, target):
power = 1
while (power < size):
power = power << 1
power = 1
index = 0
while (power):
if (index + power < size and arr[index + power] < = target):
index + = power
power >> = 1
if (arr[index] = = target):
return index
return - 1
arr = [ 1 , 3 , 5 , 7 , 8 ]
size = 5
x = 3
answer = binary_search(arr, size, x)
if (answer = = - 1 ):
print ( "Element not found" )
else :
print (f "Element found at position {answer}" )
|
C#
using System;
class GFG {
static int binary_search( int [] arr, int size,
int target)
{
int index, power;
for (power = 1; power < size; power <<= 1)
;
for (index = 0; power != 0; power >>= 1) {
if (index + power < size
&& arr[index + power] <= target)
index += power;
}
if (arr[index] == target)
return index;
return -1;
}
public static int Main()
{
int [] arr = new int [] { 1, 3, 5, 7, 8 };
int size = 5;
int x = 3;
int answer = binary_search(arr, size, x);
if (answer == -1)
Console.Write( "Element not found" );
else
Console.Write( "Element found at position "
+ answer);
return 0;
}
}
|
Javascript
<script>
function binary_search(arr, size, target)
{
let index, power;
for (power = 1; power < size; power <<= 1)
;
for (index = 0; power; power >>= 1) {
if (index + power < size
&& arr[index + power] <= target)
index += power;
}
if (arr[index] == target)
return index;
return -1;
}
let arr = [1, 3, 5, 7, 8];
let size = 5;
let x = 3;
let answer = binary_search(arr, size, x);
if (answer == -1)
document.write( "Element not found" );
else
document.write( "Element found at position " + answer);
</script>
|
OutputElement found at position 1
Time Complexity:
The time complexity of Binary Search can be written as:
T(n) = T(n/2) + c
The above recurrence can be solved either using the Recurrence Tree method or the Master method. It falls in case II of the Master Method and the solution of the recurrence is
Auxiliary Space: O(1) in case of iterative implementation. In the case of recursive implementation, O(Logn) recursion call stack space.
Algorithmic Paradigm: Decrease and Conquer.
Note:
Here we are using
int mid = low + (high – low)/2;
Maybe, you wonder why we are calculating the middle index this way, we can simply add the lower and higher index and divide it by 2.
int mid = (low + high)/2;
But if we calculate the middle index like this means our code is not 100% correct, it contains bugs.
That is, it fails for larger values of int variables low and high. Specifically, it fails if the sum of low and high is greater than the maximum positive int value(231 – 1 ).
The sum overflows to a negative value and the value stays negative when divided by 2. In java, it throws ArrayIndexOutOfBoundException.
int mid = low + (high – low)/2;
So it’s better to use it like this. This bug applies equally to merge sort and other divide and conquer algorithms.
Share your thoughts in the comments
Please Login to comment...