
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program for Binary Search
The binary search algorithm is the fastest algorithm to search for an element in a sorted array. It works by dividing the array into two halves and checking if the target element is in the left or right half, thus reducing the search space by half with each iteration.

Implementing Binary Search Algorithm
Given a sorted array of integers and a target element, and our task is to implement a python program to search for the target element in the array using the binary search algorithm. Here are some example scenarios:
Scenario 1
Input: arr[] = {1, 3, 5, 7, 9}, target = 7 Output: Element 7 found at index: 3
Scenario 2
Input: arr[] = {2, 4, 6, 8, 10}, target = 5 Output: Element 5 not found
The approaches to implement binary search are given below:
Binary Search Using Iteration
The following are the steps to search for an element using binary search with an iterative approach:
- Define a binSearch() function that accepts the array, the size of the array, and the target that you want to search.
- Then calculate the middle index of the array.
- Compare the target with the middle element using an if-else statement. Return the middle element if the target is found at the middle index.
- If the target is not available at the middle index, then check if it is greater or less than the middle element.
- If the target is greater than the middle element, then set the left index to mid + 1 and search in the right half of the array.
- If the target is less than the middle element, then set the right index to mid - 1 and search in the left half of the array.
- Repeat the above four steps until the left index becomes greater than the right index. If the element is found, return the index; otherwise, return element not found.
Example
In the following example, we are searching for an element in an array using binary search:
def binSearch(arr, n, target): left = 0 right = n - 1 while left <= right: mid = left + (right - left) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1 arr = [1, 3, 5, 7, 9] n = len(arr) target = 7 print("Given array:", arr) print("Target Element to search:", target) result = binSearch(arr, n, target) print("Element", target, "found at index:", result)
The output of the above code is as follows:
Given array: 1 3 5 7 9 Target Element to search: 7 Element 7 found at index: 3
Binary Search Using Recursion
The following are the steps to implement binary search using recursion:
- Define a binSearch() function that accepts the array, the left index, the right index, and the target that you want to search.
- Check if the left index becomes greater than the right index. If yes, return element not found.
- Then, calculate the middle index of the array and compare the target with the middle element using an if-else statement. Return the middle index if the target is found at the middle index.
- If the target is not available at the middle index, then check if it is greater or less than the middle element.
- If the target is greater than the middle element, then recursively call the binSearch() function by setting the left index as mid + 1 and searching in the right half of the array.
- If the target is less than the middle element, then recursively call the binSearch() function by setting the right index as mid - 1 and searching in the left half of the array.
- Repeat the above four steps until the left index becomes greater than the right index. If the element is found, return the index; otherwise, return element not found.
Example
The following example has a recursive function that searches for an element in a binary tree:
def binSearch(arr, left, right, target): if left > right: return -1 mid = left + (right - left) // 2 if arr[mid] == target: return mid elif arr[mid] < target: return binSearch(arr, mid + 1, right, target) else: return binSearch(arr, left, mid - 1, target) arr = [2, 4, 6, 8, 10] n = len(arr) target = 6 print("Given array:", arr) print("Target Element to search:", target) result = binSearch(arr, 0, n - 1, target) print("Element", target, "found at index:", result)
The output of the above code is as follows:
Given array: 2 4 6 8 10 Target Element to search: 6 Element 6 found at index: 2
Binary Search Using bisect Module
Python provides a built-in bisect module that contains a bisect_left and bisect_right function to perform binary search. When multiple entries of target element is found in the input list, the bisect_left function returns the index of the first occurrence, while the bisect_right function returns the next index after the last occurrence of the target element.
Example
In this example, we will use the bisect module to search for an element in a sorted array:
import bisect arr = [2, 4, 6, 6, 8, 10] target = 6 print("Index using bisect_left:", bisect.bisect_left(arr, target)) print("Index using bisect_right:", bisect.bisect_right(arr, target))
The output of the above code is as follows:
Index using bisect_left: 2 Index using bisect_right: 4
Complexity Comparison
Here is a comparison of the time and space complexity of all the above approaches.
Approach | Time Complexity | Space Complexity |
---|---|---|
Iterative Technique | O(log n) | O(1) |
Recursive Approach | O(log n) | O(log n) |
bisect Module | O(log n) | O(1) |