直接插入排序(go实现)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了直接插入排序(go实现)相关的知识,希望对你有一定的参考价值。

package main

import "fmt"

func main() {
    arr := []int{11, 2, 7, 11, 88, 91, 23, 14, 12, 33}
    straightInsertSort(arr)
    for i :=0 ; i < len(arr); i++ {
        fmt.Println(arr[i])
    }
}

func straightInsertSort(unsorted []int) {
    for i := 1; i < len(unsorted); i++ {
        if unsorted[i-1] > unsorted[i] {
            temp := unsorted[i]
            var j int
            for j = i - 1; j >= 0 && unsorted[j] > temp; j-- {
                unsorted[j+1] = unsorted[j]
            }
            unsorted[j+1] = temp
        }
    }
}


以上是关于直接插入排序(go实现)的主要内容,如果未能解决你的问题,请参考以下文章