选择排序
原理
从列表中找出最小的数,和第一个数交换位置
再从列表找第二小的数,和第二个位置交换,依此类推...
代码
import random
def select_sort(lst):
for i in range(len(lst)):
# 初始化最小值的位置
min_loc = i
for j in range(i + 1, len(lst)):
if lst[min_loc] > lst[j]:
# 获取最小值的位置
min_loc = j
# 交换位置
if min_loc != i:
lst[i], lst[min_loc] = lst[min_loc], lst[i]
lst = [random.randint(0, 10) for i in range(10)]
print(lst)
select_sort(lst)
print(lst)