go 算法之选择排序算法
2022/1/20 1:51:55
本文主要是介绍go 算法之选择排序算法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package main import ( "fmt" "strings" ) //字符串选择排序 //思路:每次选择一个相应的元素,然后将其放到指定的位置 //1. 假定最小的值得索引是i并赋值给min, //索引i对应的值和后面的索引对应的值依次比较, //如果索引i对应的值比后面索引对应的值大,则最小的索引minIndex=j func stringSelectSort(slice []string) []string { for i := 0; i < len(slice)-1; i++ { // 里面循环找到最小元素的坐标 minIndex := i // 前面排序好的,不用管,所以从i+1开始 for j := i + 1; j < len(slice); j++ { if strings.Compare(slice[minIndex], slice[j]) > 0 { //赋值 minIndex = j } } //for 循环外交换两个数值【比冒泡交换的次数少】 if i != minIndex { slice[i], slice[minIndex] = slice[minIndex], slice[i] } } return slice } func main() { value := []string{"z", "a", "b", "c", "o", "p"} res := stringSelectSort(value) fmt.Println(res) }
这篇关于go 算法之选择排序算法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26解决google chrome helper 内存占用较高!
- 2024-04-01got an unexpected keyword argument
- 2024-03-30维多利亚的秘密 golang入坑系统
- 2024-03-29mongodb sort by date
- 2024-03-29go swagger
- 2024-03-25mongodb cdc
- 2024-03-25how to use go in vscode
- 2024-03-22mongooseserverselectionerror: connect econnrefused ::1:27017
- 2024-03-21pymongo insert_many
- 2024-03-18projection mongodb