使用前提条件:数组是有序不递减数组
一个基本的二分查找,来自labuladong的算法小抄
内置函数:
lower_bound:返回数组中大于等于target的第一个索引:
index = bisect.bisect_left(nums, target)
如果返回等于数组长度,说明查找失败
内置函数:
upper_bound:返回数组中大于target的第一个索引:
index = bisect.bisect_right(nums, target)
如果返回等于数组长度,说明查找失败
labuladong算法小抄右边界:
upper_bound - 1:返回数组中大于等于target的最后一个索引:
index = bisect.bisect_right(nums, target) - 1
与bisect_right实现不同的是,这里最后返回减去了1
labuladong算法小抄左边界与bisect.bisect_left实现相同
具体实现:
def bisect_right(a, x, lo=0, hi=None):
"""Return the index where to insert item x in list a, assuming a is sorted.
The return value i is such that all e in a[:i] have e <= x, and all e in
a[i:] have e > x. So if x already appears in the list, a.insert(x) will
insert just after the rightmost x already there.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
"""
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if x < a[mid]:
hi = mid
else:
lo = mid+1
return lo
def bisect_left_(a, x, lo=0, hi=None):
"""Return the index where to insert item x in list a, assuming a is sorted.
The return value i is such that all e in a[:i] have e < x, and all e in
a[i:] have e >= x. So if x already appears in the list, a.insert(x) will
insert just before the leftmost x already there.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
"""
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if x <= a[mid]:
hi = mid
else:
lo = mid+1
return lo
def bisect_left(a, x, lo=0, hi=None):
"""Return the index where to insert item x in list a, assuming a is sorted.
The return value i is such that all e in a[:i] have e < x, and all e in
a[i:] have e >= x. So if x already appears in the list, a.insert(x) will
insert just before the leftmost x already there.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
"""
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if a[mid] < x: lo = mid+1
else: hi = mid
return lo