插入排序

栏目: 编程工具 · 发布时间: 7年前

Java代码实现

public static void insertSort(int[] arr) {
        // 开始下标
        int start = 1;
        // 数组长度
        int len = arr.length;
        for (; start < len; start++) {
            // 移动法临时变量
            int tmp = arr[start];
            int j = start - 1;
            for (; j >= 0 && arr[j] > tmp; j--) {
                arr[j + 1] = arr[j];
            }
            arr[j + 1] = tmp;
        }
    }

GoLang代码实现

func insertSort(arr []int) {
    // 从下标为1的数组开始排序
    start := 1
    // 数组长度
    len := len(arr)
    for ; start < len; start++ {
        j := start - 1
        // 移动法临时变量
        tmp := arr[start]
        for ; j >= 0 && arr[j] > tmp; j-- {
            arr[j+1] = arr[j]
        }
        arr[j+1] = tmp
    }
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

XML Hacks

XML Hacks

Michael Fitzgerald / O'Reilly Media, Inc. / 2004-07-27 / USD 24.95

Developers and system administrators alike are uncovering the true power of XML, the Extensible Markup Language that enables data to be sent over the Internet from one computer platform to another or ......一起来看看 《XML Hacks》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具