package com.atguigu.Sort; import sun.java2d.pipe.AAShapePipe; import java.util.Arrays; public class InsertSort { public static void main(String[] args) { // int[] arr = {23,45,-4,15,153}; int[] arr = new int[80000]; for(int i = 0;i < 80000;i++){ arr[i] = (int)(Math.random()*(800000 - 0 + 1)); } long start = System.currentTimeMillis(); insertSort1(arr); long end = System.currentTimeMillis(); System.out.println("花费时间为:" + (end - start)); } //插入排序 public static void insertSort(int[] arr){ int insertVal = 0; int insertIndex = 0; for(int i = 1;i < arr.length;i++){ insertVal = arr[i]; insertIndex = i - 1; while(insertIndex >= 0 && insertVal < arr[insertIndex]){ arr[insertIndex + 1] = arr[insertIndex]; insertIndex--; } if(insertIndex != i){ arr[insertIndex + 1] = insertVal; } System.out.println("第" +i+ "次排序"); System.out.println(Arrays.toString(arr)); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)