#选择排序的时间复杂度
#最优时间复杂度和最坏时间复杂度都是O(n*n)
#稳定性是不稳定的(考虑升序每次选择最大的情况
def select_sort(alist):
n = len(alist)
for j in range (n-2):
min_index = j
for i in range(j+1,n):
if alist[min_index] > alist[i]:
min_index = i
alist[j],alist[min_index] = alist[min_index],alist[j]
if __name__ == "__main__":
alist = [25, 64, 53, 23, 89, 77, 17]
print(alist)
select_sort(alist)
print(alist)
C:\Users\zhaoyuan\Anaconda3\envs\pytorch_cpu\python.exe C:/Users/zhaoyuan/PycharmProjects/untitled/数据结构与算法/选择排序.py
[25, 64, 53, 23, 89, 77, 17]
[17, 23, 25, 53, 64, 77, 89]
选择排序python实现
最新推荐文章于 2024-09-01 17:19:15 发布