def selection_sort(alist):
n = len(alist)
# 需要进行n-1次选择操作
a = 0
for i in range(n-1):
# 记录最小位置
min_index = i
exit = i
# 从i+1位置到末尾选择出最小数据
for j in range(i+1, n):
a +=1
# print('这是第%d次循环'%a)
if alist[j-1] <= alist[j]:
exit += 1
if exit + 1 == len(alist):
print('结束112')
return
if alist[j] < alist[min_index]:
min_index = j
# 如果选择出的数据不在正确位置,进行交换
if min_index != i:
alist[i], alist[min_index] = alist[min_index], alist[i]
print('这是第%d轮结束%s'%(i,alist))
print('这是第%d轮结束%s>>'%(i,alist))
# alist = [54,226,93,17,77,31,44,55,20]
alist = [1,2,2,5,7,6]
selection_sort(alist)
print(alist)
标签:sort,selection,插入排序,alist,range,print,优化
From: https://www.cnblogs.com/heris/p/16990217.html