golang-insert-sort

package main

import (
    "fmt"
)

func main() {
    fmt.Println("insert sort")
    var a = []int{6, 5, 3, 1, 8, 7, 2, 4}
    fmt.Println(a)
    InsertSort(a)
}

func InsertSort(a []int) {
    var j int
    for i := 1; i < len(a); i++ {
        for j = i; j > 0 && a[j-1] > a[j]; j-- {
            a[j], a[j-1] = a[j-1], a[j]
        }
        fmt.Println(a)
    }
}

插入排序

共 0 个回复