二分查詢:分而治之
阿新 • • 發佈:2021-04-03
def binarysearch(alist, item):
if len(alist) == 0:
return False
else:
midpoint = len(alist)//2
if alist[midpoint] == item:
return True
else:
if item<alist[midpoint]:
return binarysearch(alist[:midpoint-1], item)
else :
return binarysearch(alist[midpoint+1:], item)
a = [3, 5, 7, 10, 14, 16, 18, 20]
print(binarysearch(a, 6))