二分法 掐头结尾取中间 查找效率非常的高
# 二分法查找主要的作用就是查找元素# lst = [1,3,5,7,12,36,68,79] # 数据集 百万级数据# nl = int(input("请输入你要查找的数字:"))# left = 0# right = len(lst)-1# while left <= right:# mid = (left+right)//2# if nl > lst[mid]:# left = mid + 1# elif nl < lst[mid]:# right = mid - 1# else:# print("存在")# break# else:# print("不存在")# 递归# def func(n, lst, left, right):# mid = (left+right) // 2# if left <= right:# if n > lst[mid]:# left = mid + 1# return func(n, lst, left, right)# elif n < lst[mid]:# right = mid - 1# return func(n, lst, left, right)# else:# print("存在")# return mid# else:# print("不存在")# return -1# n = int(input("请输入你要查找的数:"))# lst = [1,3,5,7,12,36,68,79]# func(n, lst, 0, len(lst)-1)