插入排序

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

    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;
        }
    }
posted @ 2020-07-02 18:56  大猪蹄子饲养员  阅读(152)  评论(0编辑  收藏  举报