python 二分查找

 1 # coding=utf-8
 2 # python 二分查找
 3 def fn(list1, number):
 4     low = 0
 5     high = len(list1) - 1
 6     while low <= high:
 7         mid = (low + high) / 2
 8         guess = list1[mid]
 9         if guess == number:
10             return mid
11         elif guess > number:
12             high = mid - 1
13         else:
14             low = mid + 1
15     return None
16 
17 
18 my_list = [1, 3, 5, 7, 9, 11, 15]
19 print (fn(my_list, 15))

 

posted @ 2019-05-10 15:28  小嘉欣  阅读(300)  评论(0编辑  收藏  举报