golang中的排序算法实现
2021/9/16 17:08:43
本文主要是介绍golang中的排序算法实现,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1. 冒泡排序算法实现
package main import "fmt" func main() { values := []int{3, 98, 55, 46, 22, 3, 9, 18} fmt.Println(values) BubbleSortAsc(values) BubbleSortDesc(values) } func BubbleSortAsc(values []int) { for x := 0; x < len(values)-1; x++ { for y := x + 1; y < len(values); y++ { if values[x] > values[y] { values[y], values[x] = values[x], values[y] } } } fmt.Println(values) } func BubbleSortDesc(values []int) { for x := 0; x < len(values)-1; x++ { for y := x + 1; y < len(values); y++ { if values[x] < values[y] { values[y], values[x] = values[x], values[y] } } } fmt.Println(values) }
这篇关于golang中的排序算法实现的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 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