ITPub博客

首页 > IT职业 > IT图书 > 直接插入排序(go实现)

直接插入排序(go实现)

IT图书 作者:flytinge 时间:2021-09-09 10:08:21 0 删除 编辑

         

package mainimport "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        }    }}


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3914/viewspace-2817234/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2026-01-07

  • 博文量
    0
  • 访问量
    2711