Go语言实现选择法排序
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) }
输入
1, 8, 5, 9, 4, 3, 6, 6
输出
1, 3, 4, 5, 6, 6, 8, 9