The basic idea of inserting a sort (insertionsorting) is to look at the number of elements to be sorted into an ordered table and an unordered table, start an ordered table containing only one element, and the unordered table contains n-1 elements, and each time the first element is taken out of the unordered table during the sorting process. The sorting code of the ordered TABLE element is compared with the ordering code, and it is inserted into the proper position in the ordered table to make it a new ordered table.
1 Packagesorting;2 3 Public classinsertionsorting {4 //Insert Sort5 Public Static voidMain (string[] args) {6 //TODO auto-generated Method Stub7 intarr[]={12,5,17,16,19,23,14,52,13};8 9 for(inti=1;i<arr.length;i++){Ten intInsertval=Arr[i]; One //Insertval Preparation compared to the previous number A - intIndex=i-1; - while(Index>=0 && insertval<Arr[index]) { the //move the Arr[index] backward one -arr[index+1]=Arr[index]; -index--; - } + //Insert the value of the insertval in the appropriate location -arr[index+1]=Insertval; + } A //Output Results at for(intE:arr) { -System.out.print (e+ ""); - } - - } - in}
Java Insert Sort