隨着排序的進行,每次須要檢查的元素在逐漸減小,最後一次須要檢查的元素都只有一個。以下,先實現一個查找最小元素的方法,而後進行新數組的生成。數組
一、數值排序app
def findSmallest(arr):
smalllest = arr[0]
smalllest_index = 0
for i in range(1,len(arr)):
if arr[i] < smalllest:
smalllest = arr[i]
smalllest_index = i
return smalllest_index
def selecttionSort(arr):
newArr = []
for i in range(len(arr)):
smallest = findSmallest(arr)
print(arr)
newArr.append(arr.pop(smallest))
return newArr
print(selecttionSort([5,3,6,2,10]))
二、樂隊排序
def paixu(yuedui): smallest_value = yuedui[0] smallest_key = 0 for i in range(len(yuedui)): if yuedui[i][1] < smallest_value[1]: smallest_key = i return smallest_keydef solution(arr): newArry = [] for i in range(0, len(arr)): newArry.append(arr.pop(paixu(arr))) return newArryyuedui = [ ("夜曲", 30), ("晴天", 20), ("斷了的弦", 33), ("漂移", 35), ("青花瓷", 36),]print(solution(yuedui))