Binary Search tries to find an element in a sorted array. It compares the middle element of the array with the searched element. Thus we get the following cases, Case 1 : If the middle element equals the searched element, the position of the middle element is returned. Case 2 : If the middle element is bigger than the searched element, the left part of the array is searched using the same logic i.e binary search. Left part of the array : [ 0 … mid - 1 ] Case 3 : If the middle element is smaller than the searched element, the right part of the array is searched using the same logic i.e binary search. Right part of the array : [ mid + 1 … end-1 ]
Algorithm : Binary Search
1. Binary Search ( Array, Search element N ) 2. Beg = 0, End = Sizeof ( Array ) - 1 3. While (Beg <= End) 4. Mid = Beg + ( End - Beg ) / 2 5. If ( Array [ Mid ] == N ) 6. Return Mid as the index of the found element N. 7. Else If ( Array [ Mid ] < N ) 8. Beg = Mid + 1. 9. Else 10. End = Mid - 1. 11. Return -1 if the element that we are looking for i.e N is not found in the array.
Example of Binary Search
Time complexity of Binary Search : Log(N)
Why is mid calculated as mid = beg + (end-beg)/2 ?
Integer range is -2,147,483,648 to 2,147,483,647. If you are searching in an array of size 2,000,000,000 and the element searched for is located at index 1,999,999,999. When you search in the upper half of array, beg=1,000,000,001 and end=2,000,000,000. If mid is calculated as (low+high)/2, low+high = 3,000,000,001; which exceeds the range of int, resulting in overflow errors. But mid calculated as beg + (end-beg) = 1,000,000,001 + 999,999,999 = 2,000,000,000; which fits in the integer range.
Python
#!/usr/bin/python3
def BinarySearch (array, n) :
beg = 0;
end = len(array) - 1
while (beg <= end) :
mid = int(beg + (end-beg)/2);
if (array[mid] == n) :
return mid
elif (array[mid] < n) :
beg = mid + 1;
else :
end = mid - 1;
return -1;
array = [ 3, 5, 7, 11, 12, 16, 17, 52, 64, 101 ]
n = int(input("Element to search : "))
index = BinarySearch (array, n)
if (index != -1) :
print("Element found at index : " + str(index))
else :
print("Element not found. ")
Output of Binary Search implementation in Python3
Element to search : 17
Element found at index : 6
Element to search : 52
Element found at index : 7
Element to search : 64
Element found at index : 8
Element to search : 3
Element found at index : 0
C++
#include<iostream>
#include<vector>
using namespace std;
int BinarySearch (vector<int> &vec, int n) {
int beg = 0;
int end = vec.size() - 1;
while (beg <= end) {
/* Avoid calculating mid as (beg+end)/2. As (beg+end)
might exceed the range of an integer causing overflow
resulting in errors */
int mid = beg + (end-beg)/2;
if (vec[mid] == n)
return mid;
else if (vec[mid] < n)
beg = mid + 1;
else
end = mid - 1;
}
return -1;
}
int main() {
int n, index;
vector<int> vec = { 3, 5, 7, 11, 12, 16, 17, 52, 64, 101 };
cout << "Element to search : ";
cin >> n;
if((index = BinarySearch (vec, n)) != -1)
cout << "Element found at index : " << index << endl;
else
cout << "Element not found. " << endl;
return 0;
}
Output of Binary Search implementation in C++11
Element to search : 7
Element found at index : 2
Element to search : 3
Element found at index : 0
Element to search : 16
Element found at index : 5
Element to search : 23
Element not found.
Binary Search in Java.
public class Search {
int BinarySearch( int array[], int num ) {
int beg = 0;
int end = array.length - 1;
while (beg <= end) {
int mid = (beg + end) / 2;
if (array[mid] < num) {
beg = mid + 1;
} else if (array[mid] > num) {
end = mid - 1;
} else if (array[mid] == num) {
return mid;
}
}
// Return an index -1 if the number in the sorted array does not exist
return -1;
}
public static void main(String args[]) {
int[] array = { 3, 5, 7, 11, 12, 16, 17, 52, 64, 101 };
Search s = new Search();
System.out.println("Number 7 found at index : " + s.BinarySearch (array, 7));
System.out.println("Number 16 found at index : " + s.BinarySearch (array, 16));
System.out.println("Number 101 found at index : " + s.BinarySearch (array, 101));
// Binary Search return an index -1 if the number in the sorted array does not exist
System.out.println("Number 100 found at index : " + s.BinarySearch (array, 100));
}
}
Output of Binary Search implementation in Java.
Number 7 found at index : 2
Number 16 found at index : 5
Number 101 found at index : 9
Number 100 found at index : -1