编程语言
首页 > 编程语言> > binarySearch 二分法算法

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

标签:binary,arr,search,mid,binarySearch,二分法,算法,num,print
来源: https://www.cnblogs.com/ygod323/p/14965422.html