选择排序不稳定,最好、最坏和平均情况下时间复杂度都是O(n2),空间复杂度O(1)
def select_sort(lists):
lens = len(lists)
for i in range(lens):
mi = i
for j in range(i+1, lens):
if lists[mi] > lists[j]:
mi = j
lists[mi], lists[i] = lists[i], lists[mi]
return lists
if __name__ == '__main__':
lists = [3, 4, 2, 8, 9, 5, 1]
print('排序前序列为:',end='')
for i in lists:
print(i,end=' ')
print('\n排序后结果为:',end='')
for i in select_sort(lists):
print(i,end=' ')