当前位置: 代码迷 >> 综合 >> Golang选择排列
  详细解决方案

Golang选择排列

热度:16   发布时间:2024-02-21 11:42:31.0
package main
 
 
import (
    "fmt"
)
 
 
func main() {
  
    var arr = [...]int{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
    sz := len(arr)
    fmt.Printf("未排序数组")
    test(&arr, sz)
    selectSort(&arr, sz)
    test(&arr, sz)
}
 
 
func selectSort(arr *[10]int, sz int) {
  
    i := 0
    j := 0
    temp := 0
    minIndex := 0
    for i = 0; i < sz; i++ {
  
        minIndex = i
        for j = i + 1; j < sz; j++ {
  
            if arr[minIndex] > arr[j] {
  
                minIndex = j
            }
        }
 
 
        if arr[i] > arr[minIndex] {
  
            temp = arr[minIndex]
            arr[minIndex] = arr[i]
            arr[i] = temp
        }
    }
}
 
 
func test(arr *[10]int, sz int) {
  
    for i := 0; i < sz; i++ {
  
        fmt.Println(arr[i])
    }