package com.ebiz.sort; import java.text.SimpleDateFormat; import java.util.Date; /** * @author YHj * @create 2019-07-29 8:56 * 插入排序 */ public class Insert { public static void main(String[] args) { // int [] arr={101,34,119,1}; int[] arr = new int[80000]; for (int i = 0; i < 80000; i++) { arr[i] = (int) (Math.random() * 800000); } String s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); System.out.println("排序前 = " + s); getResult(arr); String l = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); System.out.println("排序后 = " + l); //System.out.println(Arrays.toString(arr)); } public static void getResult(int[] arr) { for (int i=1;i<arr.length;i++){ // definition of the number to be inserted int insertVal = ARR [I]; // before a number of index int InsertIndex =-I. 1 ; the while (InsertIndex> = 0 && ARR [InsertIndex]> insertVal) { ARR [InsertIndex + 1'd ] = ARR [InsertIndex]; InsertIndex - ; } // . found loop end to be inserted before the position of a current index ARR [InsertIndex +. 1] = insertVal; } } }
To be perfect ...