This article mainly introduces the implementation code of the python sorting algorithm. for details, refer to 1. algorithm:
For a set of keywords {K1, K2 ,..., Kn}, first from K1, K2 ,..., Select the minimum value in Kn. if it is Kz, Kz and K1 are switched;
Then, from K2, K3 ,... , Select the minimum value Kz in Kn, and then swap Kz and K2.
So select and change the n-2 trip, the (n-1) trip, from the Kn-1, Kn select the minimum Kz and Kn-1 swap, the last is the maximum value in the sequence, A small to large ordered sequence is formed in this way.
2. select the sorting code for python:
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]