08.二分查找

https://www.cnblogs.com/xiaonq/p/8573587.html

01.二分查找

l = list(range(1,101))
def bin_search(data_set,val):
   low = 0
   high = len(data_set) - 1
   while low <= high:
      mid = (low+high)//2
      if data_set[mid] == val:
         return mid
      elif data_set[mid] < val:
         low = mid + 1
      else:
         high = mid - 1
   return
n = bin_search(l,11)
print(n)            # 返回结果是: 10
posted @ 2021-03-27 20:52  小虾米爱吃鱼  阅读(31)  评论(0编辑  收藏  举报