二分查找法python代码
时间: 2023-10-14 15:28:34 浏览: 84
Python实现二分法搜索
5星 · 资源好评率100%
以下是二分查找法的Python代码:
```python
def binary_search(alist, item):
first = 0
last = len(alist)-1
found = False
while first<=last and not found:
midpoint = (first + last)//2
if alist[midpoint] == item:
found = True
else:
if item < alist[midpoint]:
last = midpoint-1
else:
first = midpoint+1
return found
```
注意:以上代码仅供参考,实际使用时可能需要根据具体情况进行修改。
阅读全文