1、开辟一个新的存储空间进行存储新的列表元素,同时循环内部的时间复杂度为n,因为用了min和remove,所以不推荐
def select_sort(li):
li_new = []
for i in range(len(li)):
min_li = min(li)
li_new.append(min_li)
li.remove(min_li)
print(li_new)
li = [1,5,2,4,9,3]
select_sort(li)
2、优化代码
def select_sort(li):
for i in range(len(li)-1):
min_loc = i
for j in range(i,len(li)):
if li[j]<li[min_loc]:
li[j],li[min_loc] = li[min_loc],li[j]
# li = [4,2,1,9,3]
#随机生成10个100范围内的元素
li = [random.randint(0,100) for i in range(1,10)]
print(li)
select_sort(li)
print(li)