Binary Search Algorithm

Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Log n).

Binary Search Algorithm: The basic steps to perform Binary Search are:
Begin with the mid element of the whole array as search key.

  • If the value of the search key is equal to the item then return index of the search key.
  • Or if the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half.
  • Otherwise, narrow it to the upper half.
  • Repeatedly check from the second point until the value is found or the interval is empty.

Step-by-step Binary Search Algorithm: We basically ignore half of the elements just after one comparison.

  1. Compare x with the middle element.
  2. If x matches with the middle element, we return the mid index.
  3. Else If x is greater than the mid element, then x can only lie in the right half subarray after the mid element. So we recur for the right half.
  4. Else (x is smaller) recur for the left half.

Java implementation of recursive Binary Search

class BinarySearch {
	int binarySearch(int arr[], int l, int r, int x)
	{
		if (r >= l) {
			int mid = l + (r - l) / 2;
			if (arr[mid] == x)
				return mid;
			if (arr[mid] > x)
				return binarySearch(arr, l, mid - 1, x);
			return binarySearch(arr, mid + 1, r, x);
		}
		return -1;
	}
	public static void main(String args[])
	{
		BinarySearch ob = new BinarySearch();
		int arr[] = { 2, 3, 4, 10, 40 };
		int n = arr.length;
		int x = 10;
		int result = ob.binarySearch(arr, 0, n - 1, x);
		if (result == -1)
			System.out.println("Element not present");
		else
			System.out.println("Element found at index "+ result);
	}
}

Python3 Program for recursive binary search.

def binarySearch(arr, l, r, x):
	if r >= l:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] > x:
			return binarySearch(arr, l, mid-1, x)
		else:
			return binarySearch(arr, mid + 1, r, x)
else:
	return -1

arr = [2, 3, 4, 10, 40] x = 10

result = binarySearch(arr, 0, len(arr)-1, x) if result != -1: print("Element is present at index % d" % result) else: print("Element is not present in array")

T.C. :: O(log n)
S.C. :: O(1)