AP CIE A-Level IGCSE

用代码
走向世界

{{ site.heroDesc }}

binary_search.py
# IGCSE · A-Level · AP 通用算法
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   # not found