码迷,mamicode.com
首页 > 编程语言 > 详细

直接插入排序(go实现)

时间:2017-10-30 21:24:32      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:排序 插入排序 直接插入排序 golang 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实现)

标签:排序 插入排序 直接插入排序 golang go

原文地址:http://11317783.blog.51cto.com/11307783/1977408

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!