python实现二分查找
def binsearch(ls,x): low = 0 high = len(ls) - 1 while low <= high: mid = (high + low) //2 if ls[mid] == x: return mid elif ls[mid] > x: high = mid - 1 else: low = mid + 1 return -1 if __name__ == '__main__': ind = binsearch(range(1,10352340) ,5345324) print(ind)
有很多坑,大家多注意,包括最大值最小值,开区间闭区间的