def selectsort(ls):
for i in range(len(ls) - 1):
max = ls[i]
#index = i
for j in range(len(ls) - 1 - i):
if ls[len(ls) - 1 - j ] > max: #找最大值
max = ls[len(ls) - 1 - j ]
#ls[len(ls) - 1 - j ] = ls[index]
ls[len(ls) - 1 - j ] = ls[i]
#ls[index] = max
ls[i] = max
return ls
ls = [1,5,4,8,3,2,9,7]
print(selectsort(ls))
结果
[9, 8, 7, 5, 4, 3, 2, 1]