Package Net.qh.test.sort;import java.util.arraylist;import Java.util.calendar;import java.util.list;/** * Created by Administrator on 2016/03/01. */public class Insert {public int[] sort (int[] arr) {if (arr = = NULL | | arr.length <= 1) {retur n Arr; } int length = Arr.length; for (int i = 1; i < length;i++) {int k = i-1; int temp = Arr[i]; while (k >= 0) {if (Arr[k] > Temp) {arr[k+1] = arr[k]; }else{break; } Arr[k] = temp; k--; }} return arr; } public static void Main (string[] args) {list<integer> intlist = new arraylist<integer> (); for (int i = 0; i < 100000; i++) {intlist.add (i); } int[] arr = new int[intlist.size ()]; for (int j = 0; J < Arr.length; J + +) {Arr[j] = Intlist.get (j); } for (int temp:arr) {System.out.print (temp + ","); } System.out.println (""); Long start = Calendar.getinstance (). Gettimeinmillis (); arr = new Insert (). sort (arr); Long end = Calendar.getinstance (). Gettimeinmillis (); System.out.println ("milliseconds required:" + (End-start)); for (int temp:arr) {System.out.print (temp + ","); }}/** 1. The sorting algorithm is a simple insert sorting algorithm. 2. Sorting ideas: Through the traversal, the smallest to find out, record the subscript, and then exchange with the destination position. 3. Compared to bubble sort: It's a very simple sort algorithm like bubble sort. When a new record is inserted into an ordered array, a sequential array is compared, the large data is moved back, and the new record is assigned the appropriate position. The bubbling sort is done with a full interchange, and the insertion sort is done in half-exchange, faster than the bubbling sort. 4. Simple selection order of sorts: O (n^2), is a stable sort. 5. Real-Combat testing: 4-core CPU and 8G of memory, 100,000 integer elements of the array sort, reverse order requires 5646 milliseconds, ordered: 5798 milliseconds */}
A sort algorithm of Java insertion sorting algorithm