二分查找

二分查找

 1 def binarySearch(l, t):
 2   low, high = 0, len(l) - 1
 3   while low < high:
 4     print low, high
 5     mid = (low + high) / 2
 6     if l[mid] > t:
 7       high = mid
 8     elif l[mid] < t:
 9       low = mid + 1
10     else:
11       return mid
12   return False
13 
14 if __name__ == '__main__':
15 l = [1, 4, 12, 45, 66, 99, 120, 444]
16   print binarySearch(l, 12)
17   print binarySearch(l, 1)
18   print binarySearch(l, 13)

 

posted @ 2020-04-23 11:18  燕十三丶  阅读(131)  评论(0编辑  收藏  举报
AmazingCounters.com