Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the basic sorting methods in golang

Shulou Source: shulou.com Published: 2022-06-02 07:45:00 09月10日 Update

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.

Tags: Sorting data fundamentals methods content more help selection yes maximum two interests guys buddies articles knowledge articles jacks references Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Huawei vpn Docker Apple