tuoheng_algN/test/算法/选择.py

22 lines
927 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
假设有一个序列a[0],a[1],a[2]...a[n]现在对它进行排序。我们先从0这个位置到n这个位置找出最小值然后将这个最小值与a[0]交换,
然后呢a[1]到a[n]就是我们接下来要排序的序列
我们可以从1这个位置到n这个位置找出最小值然后将这个最小值与a[1]交换之后a[2]到a[n]就是我们接下来要排序的序列
每一次,我们都从序列中找出一个最小值,然后把它与序列的第一个元素交换位置,这样下去,待排序的元素就会越来越少,直到最后一个
"""
def select_sort(lst):
for i in range(len(lst)):
min = i
for j in range(min, len(lst)):
# 寻找min 到len(lst)-1 这个范围内的最小值
if lst[min] > lst[j]:
min = j
lst[i], lst[min] = lst[min], lst[i]
lst = [2, 6, 1, 8, 2, 4, 9]
select_sort(lst)
print(lst)