Ai
4 Star 12 Fork 8

ShirDon-廖显东/零基础Go语言算法实战源码

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
selectionSort.go 1.10 KB
一键复制 编辑 原始数据 按行查看 历史
ShirDon-廖显东 提交于 2024-04-22 14:56 +08:00 . first commit
// ++++++++++++++++++++++++++++++++++++++++
// 《零基础Go语言算法实战》源码
// ++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// Gitee:https://gitee.com/shirdonl/goAlgorithms.git
// Buy link :https://item.jd.com/14101229.html
// ++++++++++++++++++++++++++++++++++++++++
package main
import "fmt"
// 选择排序
// 此函数使用选择排序算法对整数切片进行排序
func selectionSort(array []int) {
// 获取整数切片的长度
var length = len(array)
// 遍历切片中的每个元素
for i := 0; i < length; i++ {
// 假设当前元素是最小值
var minIdx = i
// 遍历剩余的未排序元素以找到最小值
for j := i; j < length; j++ {
// 如果找到新的最小值,则更新最小索引
if array[j] < array[minIdx] {
minIdx = j
}
}
// 用找到的最小值交换当前元素
array[i], array[minIdx] = array[minIdx], array[i]
}
}
func main() {
array := []int{99, 66, 57, 89, 36, 69, 98}
selectionSort(array)
fmt.Println(array)
}
//$ go run selectionSort.go
//[36 57 66 69 89 98 99]
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/shirdonl/goAlgorithms.git
git@gitee.com:shirdonl/goAlgorithms.git
shirdonl
goAlgorithms
零基础Go语言算法实战源码
3e77a12194dd

搜索帮助