# 用递归实现2分查找的算法,以从列表 a = [1,3,4,6,7,8,9,11,15,17,19,21,22,25,29,33,38,69,107] 查找指定的值。
a = [1, 3, 4, 6, 7, 8, 9, 11, 15, 17, 19, 21, 22, 25, 29, 33, 38, 69, 107]
def rec(num,list):
num=int(num)
n = len(list)
if n > 1 :
n = int(n / 2)
if list[n] == num:
print("%d 在列表中"%num)
elif list[n] < num:
list = list[n:]
print(list)
rec(num, list)
elif list[n] > num:
list = list[0:n]
print(list)
rec(num, list)
elif n == 1 and list[0]!= num:
print("%d 不在列表中"%num)
rec(19,a)
用递归实现2分查找的算法,以从列表查找指定的值
最新推荐文章于 2024-04-03 22:21:12 发布