1 def binary_search(number, array=[]): 2 """ binary search. """ 3 low = 0 4 high = len(array) - 1 5 while low <= high: 6 mid = (low + high) // 2 7 guess = array[mid] 8 if guess == number: 9 return mid 10 elif guess < number: 11 low = mid + 1 12 else: #guess > number 13 high = mid - 1 14 return None
二分查找法 O(logn)