# 选择排序
def findSmaillest(arr):
smallest = arr[0]
index = 0
for i in range(len(arr)):
if smallest > arr[i]:
smallest = arr[i]
index = i
return index
def selectionSort(arr):
arrNew = []
for i in range(len(arr)):
smallest = findSmaillest(arr)
arrNew.append(arr.pop(smallest))
return arrNew
print(selectionSort([5, 3, 6, 2, 10]))
[2, 3, 5, 6, 10]
注意的点:
-
选择排序时间 n 2 n^2 n2
-
arrage(3) 输出的是0.1.2。从0 开始 左闭右开
-
pop() 是删除末尾,pop(数),删除这个数位置的,而且pop有返回值,就是删的这个值
例子详见 2.3 删除
https://blog.csdn.net/wistonty11/article/details/114673314 -
append(内容) 为末尾加上内容
例子详见 2.2 添加
https://blog.csdn.net/wistonty11/article/details/114673314