What are the basic sorting methods in golang
This article introduces what are the basic sorting methods in golang, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.
Package main
Import (
"fmt"
)
Func main () {
Var (
Data = [] int {63, 10, 50, 20, 12, 19, 13, 17, 88}
)
Fmt.Println (sortMaoBao (data))
Fmt.Println (sortChoice (data))
Fmt.Println (sortInsert (data))
}
/ / sortChoice Select sort
Func sortChoice (data [] int) [] int {
Var (
L = len (data)-1
)
For l > 0 {
Var max = 0
/ / Select the largest data to arrange
For I: = 1; I
< l+1; i++ { if data[i] >Data [max] {
Max = I
}
}
Data [l], data [max] = data [max], data [l]
L Murray-
}
Return data
}
/ / sortMaoBao Bubble sort
Func sortMaoBao (data [] int) [] int {
Var (
L = len (data)-1
)
For l > 0 {
For j: = 0; j
< l; j++ { // 两个数据比较,大的进行交换数据 if data[j] >= data [juni1] {
Data [j], data [juni1] = data [juni1], data [j]
}
}
L Murray-
}
Return data
}
/ / sortInsert insert sort
Func sortInsert (data [] int) [] int {
Var (
L = len (data)-1
)
For l > 0 {
CurrentValue: = data [l]
Position: = l
For position > 0 & & data [position-1] > currentValue {
Data [position] = data [position-1]
Position = position-1
Data [position] = currentValue
}
L Murray-
}
Return data
} about what are the basic sorting methods in golang to share here, I hope the above content can be helpful to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.