PackageKpp.sort;/*** Currently to be inserted element data[i], if DATA[I]>=DATA[I-1], then the order is normal, i++ processing the next element * if DATA[I]<DATA[I-1], first save data[i] to temp, Find a suitable insertion position k, from K to i-1 element order right * Insert temp into k * * Analysis: * Binary insertion sorting is independent of the initial state of the record to be sorted and depends only on the number of records. * When n is larger, the maximum number of comparisons is much less than the direct insert sort. But the minimum number of comparisons is greater than the direct insertion sort. * The number of moves of the algorithm is the same as the direct insertion sorting algorithm, the worst case is N2/2, the best case is N, and the average number of moves is O (N2). * * @authorKPP **/ Public classMiddleinsertsort { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub intArray[] = {49,38,65,97,176,213,227,49,78,34,12,164,11,18,1}; Midinsertsort (array); for(intK:array) {System.out.println (k); } } Private Static intMidinsertsort (inta[]) { intLen =a.length; for(inti = 1;i < len;i++){ //because the sequence preceding the insertion sort is ordered, the current element is judged to be smaller than the previous element,//if small, do a binary insert sort, or if large, indicates that the current sort is correct and the next element is processed if(A[i] < a[i-1]){ //Save the value with insert first . inttemp =A[i]; //find where to insert in two points intleft = 0; intright = I-1; intMID = 0; while(Left <=Right ) {Mid= (right+left)/2; if(temp = =A[mid]) { Left=mid; Break; } Else if(Temp <A[mid]) { Right= Mid-1; }Else{ Left= Mid + 1; } } //The position to be inserted is left, and the element is moved right for(intj = I-1;j >= left;j--) {a[j+1]=A[j]; } A[left]=temp; } } return0; }}
Binary Insertion Sort Java implementation