选择排序实现
list1 = [3,5,1000,12,56,100,45,63,453]
for i in range(0,len(list1) - 1):
for j in range(i + 1,len(list1)):
if list1[i] > list1[j]:
list1[i],list1[j] = list1[j],list1[i]
print(list1)
冒泡排序
list1 = [3,5,1000,12,56,100,45,63,453]
for i in range(0,len(list1) - 1):
for j in range(0,len(list1) - i - 1):
if list1[j] > list1[j + 1]:
list1[j],list1[j + 1] = list1[j + 1], list1[j]
print(list1)
二分法排序
list1 = [3,54,545,65,465,1,23,4324]
key = 23
list1.sort()
left = 0
right = len(list1) - 1
while left <= right:
middle = (left + right) // 2
if key > list1[middle]:
left = middle + 1
elif key < list1[middle]:
right = middle - 1
else:
print("%s元素的位置为:%d" % (key,middle))
#退出循环
break