#从数组元素中找出最小值,并返回序列号
def Smallest(a): # 查找最小元素,返回索引号。
smallest = a[0]
index = 0
for i in range(1, len(a)):
if smallest > a[i]:
smallest = a[i]
index = i
return index
def Select_Sort(a):
a_sorted = []
while a: #数据不为空
smallest = Smallest(a)
a_sorted.append(a.pop(smallest))#删除待排序数组中的最小值,并将最小值赋值到排序数组中
return a_sorted
n=[2,7,5,1,11,8] #待排序数组
print("排序前:",n)
n_sorted=Select_Sort(n)
print(n_sorted)
Python编程排序-选择排序
最新推荐文章于 2022-09-19 22:40:32 发布