First, direct insertion sort
PackageAlgorithm.sort.compare.insert;Importjava.util.Arrays; Public classDirectinsertsort { Public Static voidMain (string[] args) {int[] Arraya =New int[] {11, 213, 134, 65, 77, 78, 23, 43}; Directinsertsort (Arraya); System.out.println (arrays.tostring (Arraya)); } Public Static voidDirectinsertsort (int[] Array) { for(inti = 1; i < Array.Length; i++ ){ intJ; inttemp =Array[i]; for(j = i; j > 0; j-- ){ if(Array[j-1] >temp) array[j]= Array[j-1]; Else Break; } Array[j]= temp; } } }
Second, binary insert sort
Binary Insert sort (binary insertion sort) is an improvement on the insertion sorting algorithm.
PackageAlgorithm.sort.compare.insert;Importjava.util.Arrays; Public classBinaryinsertsort { Public Static voidMain (string[] args) {int[] Arraya =New int[] {11, 213, 134, 65, 77, 78, 23, 43 }; Binaryinsertsort (Arraya); System.out.println (arrays.tostring (Arraya)); } Private Static voidBinaryinsertsort (int[] Array) { for(inti = 1; i < Array.Length; i++ ){ //if (Array[i] > array[i-1])//from big to small if(Array[i] < array[i-1]) {//from small to large intTMP =Array[i]; intLow = 0; intHigh = I-1; while(Low <=High ) { intMid = (low + high) >>> 1; //if (Array[mid] > tmp)//from big to small if(Array[mid] < TMP)//from small to largeLow = mid + 1; Else High= Mid-1; } for(intj = i; J > Low; j--) Array[j]= Array[j-1]; Array[low]=tmp; } } } }
Iii. sort of Hill
The Hill sort (Shell sort) is a sort of insertion. Also known as narrowing incremental sorting, is a more efficient and improved version of the direct insertion sorting algorithm.
PackageAlgorithm.sort.compare.insert;Importjava.util.Arrays; Public classShellsort { Public Static voidMain (string[] args) {int[] Arraya =New int[] {213, 134, 65, 77, 78, 23, 43 }; Shellsort (Arraya,0, arraya.length); System.out.println (arrays.tostring (Arraya)); } Private Static voidShellsort (int[] Array,intStartintLen) { intPower = 1; while(Power + 1) * 2 <len) Power= (Power + 1) * 2-1; for(intK = power; K >= 1; K = (k + 1)/2-1 ){ for(inti = 0; I < K; i++ ){ if(Len-i <= 1) Break; inttmp; for(intj = start + i + K; J < start + Len; J + =k) {tmp=Array[j]; intm =J; for(; m > start + i; m-=k) {if(Array[m-k] > tmp)//from small to largeARRAY[M] = array[m-K]; Else Break; } Array[m]= tmp; } } } } }
Insert sort (Direct insert sort, binary insert sort)