Skip to content
Discussion options

You must be logged in to vote

Here is a great binary search implementation 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

This runs in O(log n) time.

Replies: 1 comment

Comment options

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