Binary search (bisection) in Python

Binary search (bisection) in Python

Binary search, also known as bisection search, is an efficient algorithm for finding an element in a sorted list or array. Here's a Python implementation of binary search:

def binary_search(arr, target): left, right = 0, len(arr) - 1 while left <= right: mid = left + (right - left) // 2 # Calculate the middle index if arr[mid] == target: return mid # Element found, return its index elif arr[mid] < target: left = mid + 1 # Target is in the right half else: right = mid - 1 # Target is in the left half return -1 # Element not found in the array # Example usage: my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] target = 7 result = binary_search(my_list, target) if result != -1: print(f"Element {target} found at index {result}") else: print(f"Element {target} not found in the list") 

In this implementation:

  • arr is the sorted list or array you want to search in.
  • target is the element you're looking for.
  • left and right represent the indices of the current search range.
  • The while loop continues as long as left is less than or equal to right.
  • Inside the loop, the middle index mid is calculated, and the element at that index is compared with the target.
  • If the target is found, the function returns the index of the element.
  • If the target is less than the element at mid, the right half of the search range is discarded by updating right.
  • If the target is greater than the element at mid, the left half is discarded by updating left.
  • If the loop completes without finding the target, the function returns -1 to indicate that the target is not in the array.

This algorithm works efficiently for sorted lists, with a time complexity of O(log n), where n is the number of elements in the list.

Examples

  1. How to Implement Binary Search (Bisection) in Python

    Description: Learn how to implement the binary search algorithm (also known as bisection search) in Python. Binary search is an efficient algorithm for finding a target value within a sorted sequence.

    def binary_search(arr, target): low, high = 0, len(arr) - 1 while low <= high: mid = (low + high) // 2 if arr[mid] == target: return mid elif arr[mid] < target: low = mid + 1 else: high = mid - 1 return -1 
  2. Binary Search Implementation in Python with Recursive Approach

    Description: Explore a recursive implementation of the binary search algorithm in Python. This code snippet demonstrates how to search for a target value within a sorted array using recursion.

    def binary_search(arr, target, low=0, high=None): if high is None: high = len(arr) - 1 if low > high: return -1 mid = (low + high) // 2 if arr[mid] == target: return mid elif arr[mid] < target: return binary_search(arr, target, mid + 1, high) else: return binary_search(arr, target, low, mid - 1) 
  3. Python Code for Binary Search Algorithm with Example

    Description: Get a comprehensive Python implementation of the binary search algorithm along with an example. This code demonstrates how to perform binary search efficiently in Python.

    def binary_search(arr, target): left, right = 0, len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1 
  4. How to Use Binary Search (Bisection) in Python

    Description: Understand how to use the binary search algorithm (bisection search) in Python effectively. This code snippet demonstrates a simple example of searching for a target value in a sorted list.

    arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] target = 13 index = binary_search(arr, target) if index != -1: print(f"Target {target} found at index {index}.") else: print("Target not found.") 
  5. Binary Search Implementation for Sorted List in Python

    Description: Implement the binary search algorithm in Python specifically for a sorted list. This code snippet demonstrates how to efficiently search for a target value in a sorted sequence.

    def binary_search(arr, target): left, right = 0, len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1 
  6. Python Binary Search Function with List and Target

    Description: Use a Python function to perform binary search on a given list for a specific target value. This code snippet provides a reusable function for conducting binary search operations.

    def binary_search(arr, target): low, high = 0, len(arr) - 1 while low <= high: mid = (low + high) // 2 if arr[mid] == target: return mid elif arr[mid] < target: low = mid + 1 else: high = mid - 1 return -1 
  7. Binary Search Algorithm in Python for Sorted Array

    Description: Implement the binary search algorithm in Python to search for a target value within a sorted array efficiently. This code snippet illustrates the iterative approach to binary search.

    def binary_search(arr, target): left, right = 0, len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1 
  8. Python Binary Search Algorithm Explained with Code

    Description: Gain a comprehensive understanding of the binary search algorithm in Python through detailed explanation and code examples. This snippet illustrates the core principles behind binary search.

    def binary_search(arr, target): left, right = 0, len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1 
  9. Binary Search Implementation in Python for Sorted List

    Description: Implement the binary search algorithm in Python specifically for a sorted list. This code snippet demonstrates how to efficiently search for a target value in a sorted sequence.

    def binary_search(arr, target): left, right = 0, len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1 
  10. How to Perform Binary Search (Bisection) in Python

    Description: Understand how to perform binary search (also known as bisection search) in Python efficiently. This code snippet provides a clear implementation of the binary search algorithm.

    def binary_search(arr, target): left, right = 0, len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1 

More Tags

corpus bootstrap-treeview mouselistener osascript nested data-access-layer asp.net-core-tag-helpers mongo-java-driver dollar-quoting bitbucket-api

More Python Questions

More Livestock Calculators

More Date and Time Calculators

More Dog Calculators

More Stoichiometry Calculators