def binary_search(data,y,low=0,high=None):
high = high if high is not None else len(data)
while(low <= high):
mid = (low+high)//2
if y==data[mid]:
result = y
return result
elif y>data[mid]:
low=mid+1
else :
high=mid-1
return -1
if __name__ ==‘__main__‘:
data=[]
x=88
for i in range(0,100):
data.append(i)
index = binary_search(data,x)
print (index)
算法老师说每讲一个算法就回去编一次,跟着她的进度 不久就能熟练编程
时间: 2024-10-04 20:56:57