Golang实现选择排序

package main

import "fmt"

func main() {
    var arr []int
    arr = []int{3, 4, 9, 6, 7, 1, 2}
    res := cSort(arr)
    fmt.Println(res)
}

func cSort(arr []int) []int {
    var tmp, tmpIndex int
    count := len(arr)

    for i := 0; i < count-1; i++ {
        tmpIndex = i
        for j := i+1; j < count; j++ {
            if arr[j] < arr[tmpIndex] {
                tmpIndex = j
            }
        }
        tmp = arr[tmpIndex]
        arr[tmpIndex] = arr[i]
        arr[i] = tmp
    }
    
    return arr
}

你可能感兴趣的:(Golang实现选择排序)