def bin_search(mylist, target):
if len(mylist) == 0:
return False
mid = len(mylist) //2
min = 0
max = len(mylist) -1
while mylist[mid] != target:
print(mylist[mid])
if mylist[mid] >= target:
max = mid
mid = (min + max) //2
else:
min = mid
mid = (min + max) //2
return mid
def test_bin_search():
mylist = [1,2,3,4,5,6,7,8,9,11,13,15,16,17,19,21,23]
target = 1
assert bin_search(mylist, target) == 0
if __name__ =='__main__':
mylist = [1,2,3,4,5,6,7,8,9,11,13,15,16,17,19,21,23]
target = 8
print(bin_search(mylist,target))
网友评论