GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Variants of Binary Search
  • Bisect Algorithm Functions in Python

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Binary Search | Python - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Binary Search | Python

Last Updated : 20 Dec, 2025

Binary Search is an efficient searching algorithm used to find an element in a sorted array by repeatedly dividing the search interval in half. It reduces the time complexity to O(log N), making it much faster than linear search.

Here is working of Binary Search

  1. Find the middle element of the array.
  2. Compare the middle element with the target key. If equal: return index.
  3. If the key is smaller: search the left half.
  4. If the key is larger: search the right half.
  5. Repeat until the element is found or the search space is empty.

Binary Search using the bisect

Python provides the bisect module, which offers built-in functions to maintain a list in sorted order and perform efficient binary searches internally.

Python
import bisect

def binary_search_bisect(arr, x):
    i = bisect.bisect_left(arr, x)
    if i != len(arr) and arr[i] == x:
        return i
    else:
        return -1

arr = [2, 3, 4, 10, 40]
x = 10
result = binary_search_bisect(arr, x)

if result != -1:
    print("Element is present at index", result)
else:
    print("Element is not present in array")

Output
Element is present at index 3

Explanation:

  • bisect_left(arr, x): finds the position where x should be inserted to keep arr sorted.
  • Checks if x exists at that position; if yes, returns the index.
  • Returns -1 if the element is not found.

Iterative Binary Search

This version manually performs binary search using a while loop - efficient in both time and space.

Python
def binary_search(arr, x):
    low = 0
    high = len(arr) - 1

    while low <= high:
        mid = low + (high - low) // 2

        if arr[mid] < x:
            low = mid + 1
        elif arr[mid] > x:
            high = mid - 1
        else:
            return mid
    return -1

arr = [2, 3, 4, 10, 40]
x = 10
result = binary_search(arr, x)

if result != -1:
    print("Element is present at index", result)
else:
    print("Element is not present in array")

Output
Element is present at index 3

Explanation:

  • Using a while loop to implement iterative binary search .
  • Initialize low and high pointers to define the search range.
  • Calculates the middle index mid = low + (high - low) // 2.
  • If arr[mid] is less than x, moves search to the right half.
  • If arr[mid] is greater than x, moves search to the left half.
  • If arr[mid] == x, returns the index.
  • Returns -1 if the element is not found.

Recursive Binary Search

Recursion simplifies the logic but increases memory usage due to function call stacks.

Python
def binary_search(arr, low, high, x):
    if high >= low:
        mid = low + (high - low) // 2
        if arr[mid] == x:
            return mid
        elif arr[mid] > x:
            return binary_search(arr, low, mid - 1, x)
        else:
            return binary_search(arr, mid + 1, high, x)
    else:
        return -1

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

result = binary_search(arr, 0, len(arr) - 1, x)

if result != -1:
    print("Element is present at index", result)
else:
    print("Element is not present in array")

Output
Element is present at index 3

Explanation:

  • Using recursion to implement binary search on a sorted list.
  • Calculates the middle index and compares its value with x.
  • If x matches the middle element, returns its index.
  • If x is smaller, searches the left half; if larger, searches the right half.
  • Returns -1 if the element is not found.

Related Articles:

  • Variants of Binary Search
  • Bisect Algorithm Functions in Python
Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.