Binary Search
Binary Search
Binary search is the technique in which works efficiently on sorted lists. So, to search an element into some list using the binarysearch method, we must sure that the list is to be sorted. This is a searching algorithm to finding an element’s position in a sorted array.
This algorithm follows the divide and conquer approach in which the list is divided into two parts, and the item is compared with the middle element of the list. If this element match is found then, the location of the middle element is returned. Otherwise, we search into either of the halves depend upon the result produced throughout the match.
Algorithm
Binary_Search(a, lower_bound, upper_bound, val) Step 1: set beg = lower_bound, end = upper_bound, pos = - 1 Step 2: repeat the steps 3 and 4 while beg <=end Step 3: set mid = (beg + end)/2 Step 4: if a[mid] = val set pos = mid print pos go to step 6 else if a[mid] > val set end = mid - 1 else set beg = mid + 1 [end of if] [end of loop] Step 5: if pos = -1 print "value is absent in the array" [end of if] Step 6: exit
Working of Binary search
Working of the BinarySearch Algorithm.
Firstly we take a sorted array.
There are two methods to implement the binarysearch algorithm they arw follows-
-
- Iterative method
- Recursive method
The recursive method of binarysearch follows the divide and conquer approach for searching.
Let the elements of array are given below
Suppose the element to search is, K = 56
We have to use the following formula to calculate the mid of the array –
- mid = (beg + end)/2
So, in the given array is
beg = 0
end = 8
mid = (0 + 8)/2 = 4. Hence , 4 is the mid of the array.
Now, the element to be the search is found. So this algorithm will return the index of the element matched.
Program for binary search in C
#include <stdio.h> int binarySearch(int array[], int x, int low, int high) { while (low <= high) { int mid = low + (high - low) / 2; if (array[mid] == x) return mid; if (array[mid] < x) low = mid + 1; else high = mid - 1; } return -1; } int main(void) { int array[] = {3, 4, 5, 6, 7, 8, 9}; int n = sizeof(array) / sizeof(array[0]); int x = 4; int result = binarySearch(array, x, 0, n - 1); if (result == -1) printf("Not found"); else printf("Element is found at index %d", result); return 0; }
Output
Element is found at index 1
Program for binarysearch in C++
#include <iostream> using namespace std; int binarySearch(int array[], int x, int low, int high) { while (low <= high) { int mid = low + (high - low) / 2; if (array[mid] == x) return mid; if (array[mid] < x) low = mid + 1; else high = mid - 1; } return -1; } int main(void) { int array[] = {3, 4, 5, 6, 7, 8, 9}; int x = 4; int n = sizeof(array) / sizeof(array[0]); int result = binarySearch(array, x, 0, n - 1); if (result == -1) printf("Not found"); else printf("Element is found at index %d", result); }
Output
Element is found at index 1
Program for binarysearch in Python
def binarySearch(array, x, low, high): while low <= high: mid = low + (high - low)//2 if array[mid] == x: return mid elif array[mid] < x: low = mid + 1 else: high = mid - 1 return -1 array = [3, 4, 5, 6, 7, 8, 9] x = 4 result = binarySearch(array, x, 0, len(array)-1) if result != -1: print("Element is present at index " + str(result)) else: print("Not found")
Output
Element is present at index 1
Program for binary search in Java
class BinarySearch { int binarySearch(int array[], int x, int low, int high) { while (low <= high) { int mid = low + (high - low) / 2; if (array[mid] == x) return mid; if (array[mid] < x) low = mid + 1; else high = mid - 1; } return -1; } public static void main(String args[]) { BinarySearch ob = new BinarySearch(); int array[] = { 3, 4, 5, 6, 7, 8, 9 }; int n = array.length; int x = 4; int result = ob.binarySearch(array, x, 0, n - 1); if (result == -1) System.out.println("Not found"); else System.out.println("Element found at index " + result); } }
Output
Element found at index 1
Binary Search complexity
1. Time Complexity
Case | Time Complexity |
---|---|
Best Case | O(1) |
Average Case | O(logn) |
Worst Case | O(logn) |
- Best Case Complexity – The best-case time complexity is O(1).
- Average Case Complexity – The average case time complexity is O(logn).
- Worst Case Complexity – The worst-case time complexity is O(logn).
2. Space Complexity
Space Complexity | O(1) |
- The space complexity is O(1).
Binary Search Applications
- In libraries of Java, .Net, C++ STL
- While in debugging, the binarysearch is used to pinpoint the place where the error occurs.
Read more, Linear search