python数据结构基础知识点二分查找

a=[2,7,1,3,5,8,3,6,7,15,34]
for i in range(len(a)-1):
    for j in range(i+1,len(a)):
        if a[i]>a[j]:
            a[i],a[j]=a[j],a[i]
print(a)
while True:
    num=int(input('请输入一个数'))
    first=0
    last=len(a)-1
    while first<=last:
        mid=(last+first)//2
        if a[mid]==num:
            print(num,'找到了')
            break
        elif a[mid] <num:
            first=mid+1
        else:
            last=mid-1
    else:
        print('没找到')


```![在这里插入图片描述](https://img-blog.csdnimg.cn/20190315165208972.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjIxODg2OA==,size_16,color_FFFFFF,t_70)

猜你喜欢

转载自blog.csdn.net/weixin_42218868/article/details/88578699