二分法查找

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 @ 2020-08-12 08:13  Python代  阅读(47)  评论(0编辑  收藏  举报