Go language implementation selection sort instances

  • 2020-05-12 02:46:43
  • OfStack

This article illustrates an example of how the Go language implements selection ordering. Share with you for your reference. The specific implementation method is as follows:

package main
import "fmt"
func select_sort(a []int) {
 len := len(a)
 for i:=0; i < len-1; i++ {
  k := i
  j:= i + 1  
  for ; j < len; j++ {
   if a[j] < a[k] { k = j }
  }
  if k != i {
   a[i], a[k] = a[k], a[i]
  }
 }
} func print_array(a []int) {
 for i := 0; i < len(a) - 1; i++ {
  fmt.Printf("%d, ", a[i])
 }
 fmt.Print(a[len(a)-1])
} func main() {
 a := []int{1, 8, 5, 9, 4, 3, 6, 6}
 print_array(a)
 fmt.Printf("\n")
 select_sort(a)
 print_array(a)
}

Input:

1, 8, 5, 9, 4, 3, 6, 6

Output:

1, 3, 4, 5, 6, 6, 8, 9

I hope this article has helped you with the programming of Go language.


Related articles: