概念
选择排序是一种数组的排序方法。其时间复杂度为O(n2)。
程序代码
Python语言
def select_sort_simple(li: list):
li_new = []
for i in range(len(li)):
min_val = min(li)
li_new.append(min_val)
li.remove(min_val)
return li_new
def select_sort(li):
for i in range(len(li) - 1):
min_loc = i
for j in range(i + 1, len(li)):
if li[j] < li[min_loc]:
min_loc = j
if min_loc != i:
li[i], li[min_loc] = li[min_loc], li[i]
print(li)
li = [4, 6, 3, 2, 1, 5]
print(li)
select_sort(li)