选择排序就是遍历一个列表,从第一个开始和后面的进行比较,找到比第一个小的,调换位置。
def sort_list(li):
for i in range(len(li)):
min_index = i
for j in range(i+1,len(li)):
if li[min_index]>li[j]:
li[j],li[min_index] = li[min_index],li[j]
print(li)
list = [3,2,1,10,9,32,45,20,50]
sort_list(list)
执行结果:
[1, 3, 2, 10, 9, 32, 45, 20, 50]
[1, 2, 3, 10, 9, 32, 45, 20, 50]
[1, 2, 3, 10, 9, 32, 45, 20, 50]
[1, 2, 3, 9, 10, 32, 45, 20, 50]
[1, 2, 3, 9, 10, 32, 45, 20, 50]
[1, 2, 3, 9, 10, 20, 45, 32, 50]
[1, 2, 3, 9, 10, 20, 32, 45, 50]
[1, 2, 3, 9, 10, 20, 32, 45, 50]
[1, 2, 3, 9, 10, 20, 32, 45, 50]