基础介绍
- 将一个数组看做成一个有序列表和一个无序列表
- 初始化有序列表最后一位的下表为: 0
- 初始化无需列表第一个的下标为: 1
- 将无需列表的第一个 挨个与有序列表做比较
- 当无序列表第一个小于当前有序列表的时候 , 将当前有序向后挪一位
- 继续将无序列表与

package com.h.排序;import com.h.util.ArrayUtil;/** * 排序算法 * 2022/6/6 */public class InsertSort { static int[] array; InsertSort(int[] array){ this.array = array; } //有序列表的下最后一位的下标 static int listIndex = 0; static int setIndex = 1; public static void main(String[] args) { ArrayUtil arrayUtil = new ArrayUtil(); array = arrayUtil.crateArray(100000,500000); arrayUtil.start(); ArrayUtil.sout(array); run(); arrayUtil.sout(array); arrayUtil.end(); } public static void run(){ while (listIndex < array.length-1) { int localListIndex = listIndex; int localSetIndex = setIndex; //防止数组下标越界 while (localListIndex >= 0) { if (array[localSetIndex] < array[localListIndex]) { int saveValue = array[localListIndex]; array[localListIndex] = array[localSetIndex]; array[localSetIndex] = saveValue; } localListIndex--; localSetIndex--; } listIndex++; setIndex++; } }}