Skip to content
Discussion options

You must be logged in to vote

Binary Search works on sorted arrays by repeatedly dividing the search interval in half. Here is a simple implementation:

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

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ammiyo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants