Principle
Simple selection of sorting should be the most intuitive and easy to understand sorting method.
Directly from the beginning of one to go than, find the smallest to put to the far left. Then finish sorting the other bits in turn.
Complexity of Time
The number of comparisons is fixed to O (n^2) and the number of data exchanges is 0~n-1 times
Select sort is not stable because data is exchanged for the same values in different locations
Code implementation
package mainimport ( "fmt")func main() { var length = 10 var mm = make(map[int]int, length) var o []int // 先准备一个顺序随机的数(qie)组(pian) for i := 0; i < length; i++ { mm[i] = i } for k, _ := range mm { o = append(o, k) } fmt.Println(o) // 两次循环找到最小的排前面 for i := 0; i < length; i++ { min := i for j := length - 1; j >= i; j-- { if o[j] < o[min] { min = j } } // 当前就是最小值时就不交换了 if min == i { continue } // 顺序交换 o[i] = o[i] + o[min] o[min] = o[i] - o[min] o[i] -= o[min] fmt.Println(o) }}
Run results