# -*- coding:utf8 -*-
def bin_search(items, key):
'''折半查找'''
start, end = 0, len(items) - 1
while start <= end:
mid = (start + end) // 2
if key > items[mid]:
start = mid + 1
elif key < items[mid]:
end = mid - 1
else:
return mid
return -1
print(bin_search([3.2,2,10,3,1,45,90,0], 3))
Python 折半查找算法代码实现
最新推荐文章于 2024-09-26 16:52:48 发布
本文深入探讨了折半查找算法的实现原理与过程,通过一个具体的Python代码示例,详细解释了如何在有序数组中搜索特定元素。折半查找,又称二分查找,是一种高效的搜索算法,适用于已排序的数据集。


690

被折叠的 条评论
为什么被折叠?



