In this tutorial, you will learn how the Binary Search sort works. Likewise, you will discover working instances of Binary Search in C, C++, Java, and Python.
In this article, you will learn-
What is Binary Search?
A Binary search is an advanced type of search algorithm that finds and fetches data from a sorted list of items. Its center working rule includes dividing the data in the list in half until the necessary value is found and shown to the user in the search result. A binarysearch is normally known as a half-interval search or a logarithmic search.
BinarySearch is a searching algorithm for finding an element’s situation in a sorted array.
In this methodology, the element is constantly searched in a part of an array.
Binarysearch can be actualized distinctly on a sorted list of items. In the event that the elements are not sorted as of now, we need to sort them first.
Binary Search Working
BinarySearch Algorithm can be actualized in two different ways which are talked about underneath.
- Iterative Method
- Recursive Method
The recursive strategy follows the divide and conquer approach.
The general steps for the two techniques are examined underneath.
- The array in which searching is to be performed is:
Let x = 4 be the element to be searched.
2. Set two pointers low and high at the lowest and the highest positions respectively.
3. Locate the middle element mid of the array ie. (arr[low + high])/2 = 6.
4. On the off chance that x == mid, return mid. Else, compare the element with be searched with m.
5. On the off chance that x > mid, compare x and the middle element of the elements on the right side of mid. This is finished by setting low to low = mid + 1.
6. Else, compare x and the middle element of the elements on the left side of mid. This is finished by setting high to high = mid – 1.
7. Repeat steps 3 to 6 until low meets high.
8. x = 4 is found.
Binary Search Algorithm
Iteration Method
do until the pointers low and high meet each other. mid = (low + high)/2 if (x == arr[mid]) return mid else if (x > A[mid]) // x is on the right side low = mid + 1 else // x is on the left side high = mid - 1
Recursive Method
binarySearch(arr, x, low, high) if low > high return False else mid = (low + high) / 2 if x == arr[mid] return mid else if x < data[mid] // x is on the right side return binarySearch(arr, x, mid + 1, high) else // x is on the right side return binarySearch(arr, x, low, mid - 1)
Python, Java, C/C++ Examples (Iterative Method)
Python
# Binary Search in python def binarySearch(array, x, low, high): # Repeat until the pointers low and high meet each other 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")
Java
// Binary Search in Java class BinarySearch { int binarySearch(int array[], int x, int low, int high) { // Repeat until the pointers low and high meet each other 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); } }
C
// Binary Search in C #include <stdio.h> int binarySearch(int array[], int x, int low, int high) { // Repeat until the pointers low and high meet each other 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; }
C++
// Binary Search in C++ #include <iostream> using namespace std; int binarySearch(int array[], int x, int low, int high) { // Repeat until the pointers low and high meet each other 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); }
Python, Java, C/C++ Examples (Recursive Method)
Python
# Binary Search in python def binarySearch(array, x, low, high): if high >= low: mid = low + (high - low)//2 # If found at mid, then return it if array[mid] == x: return mid # Search the left half elif array[mid] > x: return binarySearch(array, x, low, mid-1) # Search the right half else: return binarySearch(array, x, mid + 1, high) else: 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")
Java
// Binary Search in Java class BinarySearch { int binarySearch(int array[], int x, int low, int high) { if (high >= low) { int mid = low + (high - low) / 2; // If found at mid, then return it if (array[mid] == x) return mid; // Search the left half if (array[mid] > x) return binarySearch(array, x, low, mid - 1); // Search the right half return binarySearch(array, x, mid + 1, high); } 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); } }
C
// Binary Search in C #include <stdio.h> int binarySearch(int array[], int x, int low, int high) { if (high >= low) { int mid = low + (high - low) / 2; // If found at mid, then return it if (array[mid] == x) return mid; // Search the left half if (array[mid] > x) return binarySearch(array, x, low, mid - 1); // Search the right half return binarySearch(array, x, mid + 1, high); } 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); }
C++
// Binary Search in C++ #include <iostream> using namespace std; int binarySearch(int array[], int x, int low, int high) { if (high >= low) { int mid = low + (high - low) / 2; // If found at mid, then return it if (array[mid] == x) return mid; // Search the left half if (array[mid] > x) return binarySearch(array, x, low, mid - 1); // Search the right half return binarySearch(array, x, mid + 1, high); } 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); }
Binary Search Complexity
Time Complexities
- Best case complexity: O(1)
- Average case complexity: O(log n)
- Worst case complexity: O(log n)
Space Complexity
The space complexity of the binary search is O(n).
Binary Search Applications
- In libraries of Java, .Net, C++ STL
- While debugging, the binarysearch is used to pinpoint where the mistake occurs.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.