Insert Sort:
Stable sorting algorithm
The elements of the comparison begin with the previous two, then the first three, up to n
The direction of the comparison is from the backward forward comparison, satisfies the comparison condition: the previous assignment to the latter one, a round comparison, the right boundary value assigned to the left 1th not meet the conditions of the position
The number of comparisons is 1,2,3...n-1, in total (n^2-n), the number of assignments is similar to the number of comparisons, may be slightly greater, but one-way assignment is definitely faster than switching (bidirectional assignment).
Efficiency: Although still O (n^2), the insertion sort is much better for partially ordered data. It's usually a little faster than bubbles, slightly faster than the selection.
/** * Insert Sort * @author Stone * */public class Insertsort {public static void main (string[] args) {int len = 10;int[] ary = n EW Int[len]; Random random = new random (), for (int j = 0; J < Len; J + +) {Ary[j] = random.nextint (1000);} SYSTEM.OUT.PRINTLN ("-------------before sorting");//ary=new int[]{10,9,8,7,6,5,4,3,2,1}; Test//ary=new int[]{1,2,3,4,5,6,7,8,10,9}; Test for (int j = 0; J < Ary.length; J + +) {System.out.print (Ary[j] + "");} INSERTASC1 (ary);//With switching operation, the efficiency is slightly lower insertAsc2 (ary);//This is a good notation, using one-way assignment//insertasc3 (ary);//As with 2 logic, write while change for}/* * The elements of the comparison begin with the previous two, then the first three, up to N * Compare the direction of the comparison from backward forward, satisfying the comparison condition then exchanging * stable sorting algorithm * Comparison of the number of times is 1,2,3...n-1, Total (n^2-n) */static void InsertAsc1 (int[] ar Y) {int comparecount = 0;//number of comparisons int changecount = 0;//interchange int len = ary.length; System.out.println (""); for (int i = 1; i < Len; i++) {//each round of comparison the first i+1 elements are ordered for (int j = i; j > 0; j--) {if (Ary[j] < ary[j-1]) {//The right side is smaller than the left, swap position ary[j] = ary[j-1] + (Ary[j-1] = ary[j]) * 0;changecount++;comparecount++;} else {break;}}} System.out.println ("\ n-------insertAsc1 in ascending order------comparison:" + Comparecount + ", Number of Exchanges:" + Changecount); for (int j = 0; J < Ary.length; J + +) {System.out.print (Ary[j] + "");}} static void InsertAsc2 (int[] ary) {int comparecount = 0;//number of comparisons int setvaluecount = 0;//assignment number int len = ary.length; System.out.println (""); for (int i = 1; i < Len; i++) {int temp = Ary[i];int J = i;while (J > 0 && ary[j-1] >= temp) {Ary[j] = ary[j-1];//0 assigned to 1, 1 assigns to 2 ... j--;comparecount++;setvaluecount++;} if (ary[j]! = temp) {Ary[j] = temp;//The right boundary value of this round is assigned to the left setvaluecount++ after the comparison;}} System.out.println ("\ n-------insertAsc2 in ascending order------comparison:" + Comparecount + ", Number of assignments:" + Setvaluecount); for (int j = 0; J < Ary.length; J + +) {System.out.print (Ary[j] + "");}} static void InsertAsc3 (int[] ary) {int comparecount = 0;//compare number of times int setvaluecount = 0;//the number of assignments int len = ary.length; System.out.println (""); int temp;for (int i = 1; i < Len; i++) {temp = Ary[i];int j = i;for (; j > 0; j--) {if (temp < ary[j-1]) {Ary[j] = Ary[j-1];comparecount++;setvaluecount++;} else {break;}} if (ary[j]! = temp) {Ary[j] = temp;setvaluecount++;}} System.out.println ("\ n-------insertAsc3 in ascending order------comparison:" + Comparecount + ", Number of assignments:" + Setvaluecount); for (int j = 0; J < Ary.length; J + +) {System.out.print (Ary[j] + "");}}}
Print
Java Implementation Insert Sort