defbinary_serch(list_,item):
low=0
high=len(list_)-1if list_[-1]==item:return high
while low<high:
num=low+int((high-low)/2)#向下取整
guess=list_[num]if guess==item:return num
if guess>item:
high=num
if guess<item:
low=num
# item_=listif __name__=='__main__':
a=[1,4,6,8,10,11,23,100,230,1000]print(binary_serch(a,6))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
冒泡排序
defbubble_sort(list_):
num=len(list_)for i inrange(num):# print(i,num)for j inrange(num-i-1):print(j)
m=list_[j]
n=list_[j+1]if m>n:
list_[j+1]=m
list_[j]=n
return list_
if __name__=='__main__':
a=[1,4,6,8,10,11,23,100,230,1000]print(bubble_sort(a))