用go语言来玩下选择排序

    用go语言来玩下选择排序, 找找go的感觉:

package main
import "fmt"

func select_sort(a []int){
    n := len(a)
    for i := 0; i < n; i++ {
        pivot := i;
        pivotKey := a[i];
        for j := i + 1; j < n; j++ {
            if a[j] < pivotKey {
                pivot = j;
                pivotKey = a[j];
            }
        }
        
        a[pivot], a[i] = a[i],  a[pivot]
    }
    
}

func print(a []int){
    for _, v := range a {
        fmt.Println(v)
    }
}

func main(){
    var a []int = []int{0, 3, 6, -1, 2, 5, 4, 8, 9}
    select_sort(a)
    print(a)
}

        结果: 

-1
0
2
3
4
5
6
8
9
 

     不多说。

猜你喜欢

转载自blog.csdn.net/stpeace/article/details/81665103