def binarysearch(A,x):
low=0
high=len(A)-1
j=0
i=0
while low<=high and j==0:
mid=(low+high)/2
if x==A[mid]:
j=mid
elif x<A[mid]:
high=mid-1
else:
low=mid+1
i=i+1
print 'use %d times'%i
return j
'''
B=[1,4,5,7,8,9,10,12,15,22,23,27,32,35]
C=[(i+10) for i in range(1,61)]
print binarysearch(B,35)
print binarysearch(B,36)
print binarysearch(C,70)
print binarysearch(C,77)
'''
将书上伪代码用py写了,拿书上的例题测试了,好像没问题。
algorithm_binarysearch
最新推荐文章于 2021-07-11 08:34:29 发布