python写一个二分查找
时间: 2023-03-08 08:08:25 浏览: 93
我可以给你一个Python实现的二分查找示例:def binary_search(alist, item):
first = 0
last = len(alist) - 1
found = False while first <= last and not found:
mid = (first + last) // 2
if alist[mid] == item:
found = True
else:
if item < alist[mid]:
last = mid - 1
else:
first = mid + 1
return found print(binary_search([1,2,3,4,5], 4)) # True
阅读全文