Binary search in python
This commit is contained in:
parent
a578d31d8c
commit
fde9817fba
22
python/15_bsearch/bsearch.py
Normal file
22
python/15_bsearch/bsearch.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
"""
|
||||||
|
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
|
Loading…
Reference in New Issue
Block a user