algo/python/15_bsearch/bsearch.py

23 lines
529 B
Python
Raw Normal View History

2018-10-26 03:41:01 +08:00
"""
Author: Wenru
"""
from typing import List
def bsearch(nums: List[int], target: int) -> int:
"""Binary search of a target in a sorted array
without duplicates. If such a target does not exist,
return -1, othewise, return its index.
"""
low, high = 0, len(nums) - 1
while low <= high:
mid = low + (high - low) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1