选择排序(从小到大)
大概思路:
找出初始数组中最小值,放到一个新数组中,一直重复。
def findSmallest(arr
):
smallest
= arr
[0]
smallest_index
= 0
for i
in range(1,len(arr
)):
if arr
[i
] < smallest
:
smallest
= arr
[i
]
smallest_index
= i
return smallest_index
def selectionSort(arr
):
newArr
= []
for i
in range(len(arr
)):
smallest
= findSmallest
(arr
)
newArr
.append
(arr
.pop
(smallest
))
return newArr
print(newArr
[])
arr
= [9,8,6,4]
selectionSort
()
转载请注明原文地址:https://tech.qufami.com/read-14608.html