binarySearch 二分法算法

# 二分法算法

def binary_search(arr, num):
if len(arr)==0:
print('该值不存在')
return False

mid = int(len(arr)/2)
print(mid)
if arr[mid] > num:
arr = arr[:mid-1]
binary_search(arr,num)

elif arr[mid] < num:
arr = arr[mid:]
binary_search(arr,num)

else:
# num_index = arr.index(arr[mid])
print(f'找到它了{arr[mid]}')
return True


list1 = [2,3,4,5,6,7,8,9,23,34,888,8545,67654]

# list1 = []

num = 5
result = binary_search(list1,num)
print(result)

参考路径:https://www.cnblogs.com/liunaixu/p/12673211.html

上一篇:98. Validate Binary Search Tree 检验二叉树是不是bst


下一篇:activemq,rabbitmq, kafka, rocketmq区别