Loading... # 选择排序 ## Golang 实现 ```go package main import "fmt" func main() { arr := [] int16{6, 5, 3, 1, 8, 7, 2, 4} fmt.Println("select sort --------") sortBySelectSort := selectSort(arr) fmt.Println(sortBySelectSort) } /** 选择排序是一种不稳定的排序,需要记录位置, */ func selectSort(arr []int16) []int16 { length := len(arr) //从第一个元素开始 for i := 0; i < length; i++ { position := i //从标志位的后一位开始选择 for j := i + 1; j < length; j++ { //有比标志位小的记住位置,直到找到最小的 if arr[j] < arr[position] { position = j } } if position != i { //找到这一轮最小的跟标志位对换 arr[position] = arr[i] + arr[position] arr[i] = arr[position] - arr[i] arr[position] = arr[position] - arr[i] } } return arr } ``` ## 图解 [![iQISB9.md.gif](https://gz-blog-storage-1252787757.cos.ap-guangzhou.myqcloud.com/usr/uploads/2024/05/17162846574837.gif)](https://imgchr.com/i/iQISB9) ## Complexity | Name | Best | Average | Worst | Memory | Stable | Comments | | ------------------ | :-----------: | :-----------: | :-----------: | :----: | :----: | :------- | | **Selection sort** | n<sup>2</sup> | n<sup>2</sup> | n<sup>2</sup> | 1 | No | | © Allow specification reprint Support Appreciate the author AliPayWeChat Like If you think my article is useful to you, please feel free to appreciate