1. Algorithm.
For a set of keywords {K1,K2,...,Kn}, first select the smallest value from K1,K2,...,Kn and if it is Kz, then swap Kz with K1;
Then choose the minimum value Kz from K2, K3, ... , Kn and then swap Kz with K2.
This is done by selecting and swapping n-2 trips, and on the (n-1)th trip, the smallest value Kz from Kn-1, Kn is selected Kz is swapped with Kn-1, and what is left at the end is the largest value in the sequence, and a small-to-large ordered sequence is formed in this way.
Select the sort code:
Copy Code The code is as follows.
def selection_sort(list2):
for i in range(0, len (list2)):
min = i
for j in range(i + 1, len(list2)):
if list2[j] < list2[min]:
min = j
list2[i], list2[min] = list2[min], list2[i] # swap
Result: [2, 3, 4, 21, 33, 44, 45, 67]