首页 > 编程语言 > 详细

插入排序

时间:2020-07-02 19:49:11      阅读:61      评论:0      收藏:0      [点我收藏+]

从左开始遍历,找到比当前值小的后一个索引位置,然后插入

    public void insertSort(int[] num){
        for (int i = 1; i < num.length; i++) {
            int preIndex = i - 1;
            int currentVal = num[i];
            while(preIndex >= 0 && num[preIndex] > currentVal){
                num[preIndex+1] = num[preIndex];
                preIndex--;
            }
            num[preIndex+1] = currentVal;
        }
    }

插入排序

原文:https://www.cnblogs.com/wliamchen/p/13226463.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!