Selection order:
selectionSort-selection sort: select them out and sort them together .
-
Ascending order: Select the smallest value in the meta container and stand in order.
# 选出最小值 def findMinimum(arr): # Stores the Minimum minN = arr[0] # Stores the index of the Minimum minN_index = 0 for i in range(1, len(arr)): if arr[i] < minN: minN_index = i minN = arr[i] return minN_index # 选择排序 def selectionSort(arr): newArr = [] for i in range(len(arr)): # 选最小值,排一起 smallest = findMinimum(arr) newArr.append(arr.pop(smallest)) return newArr print(selectionSort([5, 3, 6, 2, 10]))
Output print:
[2, 3, 5, 6, 10]
-
Descending order
Just change the minimum value to the maximum valuedef findMaximum(arr): maxN = arr[0] maxN_index = 0 for i in range(1, len(arr)): if arr[i] > maxN: maxN_index = i maxN = arr[i] return maxN_index
Tip: A special arr.pop(smallest)
sentence is required , which means that the minimum value found needs to be removed from the original array and will not participate in the next selection.