1. Insert Sort
The insertion sort is one of the simplest sorting algorithms, consisting of N-1, which must be guaranteed that the elements at position 0 to P are sorted.
The specific implementation code is as follows:
PackageK;ImportJava.util.Scanner; Public classTest2 { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); //get the input string and remove all the spaces in the middleString [] str = In.nextline (). Split ("\\s+"); int[] inter=New int[Str.length]; //Convert a string to a number for(inti=0;i<inter.length;i++) {Inter[i]=integer.valueof (Str[i]); } //Sortsort (inter); //results after sorting the output for(inti=0;i<inter.length;i++) {System.out.printf ("%d", Inter[i]); } } /*** Sort *@paramInter*/ Private Static voidSortint[] Inter) { intp; for(p=1;p<inter.length;p++) { intJ; inttemp=Inter[p]; for(J=p-1;j>=0 && temp<inter[j];j--) {inter[j+1]=Inter[j]; } inter[j+1]=temp; } }}
The insertion sort is implemented in sort ().
The principle can be illustrated with one of the following pictures:
The insertion sort algorithm is implemented above. The time complexity is O (N).
Java sorting algorithm