def smallest(lis): """ 取列表最小值 :param lis: 期望排序的列表 :return: 最小值的索引位置 :smallest:存储最小值 :smallest_index:最小值的索引位置 """ smallest = lis[0] smallest_index = 0 for i in range(0, len(lis)): if lis[i] < smallest: smallest = lis[i] smallest_index = i return smallest_index def selection_sort(lis): """ 选择排序后的列表 :param lis: 期望排序的列表 :return: 排序完成的列表 """ new_sort = [] for i in range(0, len(lis)): smallest_index = smallest(lis) new_sort.append(lis.pop(smallest_index)) return new_sort li = [5, 8, 3, 4, 1, 6, 9, 7, 4, 2] print(selection_sort(li))