package lsn1.sjjg.cct.cn.lsn1; import org.junit.Test; /** * 插入排序 * Created by linyaokui on 17/12/8. */ public class InsertSort { @Test public void test(){ int[] array=new int[]{3,9,1,2,5,4,7,8,6}; insertSort(array); for (int i : array){ System.out.print(i + " "); } } /** * 插入排序 * @param array */ public void insertSort(int[] array){ for(int i=1;i<array.length;i++){ int target = array[i]; // 要插入的目标数据 int j = i; while (j > 0 && array[j-1] > target){ // 如果j移动到0 或者发现比它小的就停止插入 array[j] = array[j-1]; j--; } array[j] = target; //执行插入 } } }
插入排序
最新推荐文章于 2024-09-14 19:06:02 发布