Binary Insertion Sort
Rationale: For an improvement to a direct insertion sort, you can quickly determine the insertion position of the first element by continually binary. That is, the middle point of the 0~i-1 is computed, and the elements at the I index are compared with the median. If the element at the I index is large, it means that the element to be inserted should be on the right side of the middle value, whereas on the left (small to large)
time efficiency: O (n2) O (n^2)
Space efficiency: O (1) O (1)
Algorithm Stability: unstable
Binary insertion sort (small to large sort) public class Sort_binary {private void Binarysort (int nums[]) {int length = Nums.length;
int minindex = 0; for (int i = 1;i<length;i++) {//start with the second element int left=0;//left pointer int right=i-1;//right pointer int tem p = nums[i];//the element to which the loop is to be inserted//To determine the position index while (left<=right) {Minindex = (left+rig
HT)/2;
if (Nums[minindex]<nums[i]) {left = minindex+1;
}else{right = minIndex-1; }//move the original array element after the insertion position index for (int k =i;k>left;k--) {nums[k] = Nums[k-1
];
}//Update element Nums[left for insert location index = temp;
} System.out.println (Java.util.Arrays.toString (nums));
}//test function public static void main (string[] args) {int nums[]={89,54,78,8,398,7,560,25,963,16};
Sort_binary test = new Sort_binary (); Test.binarysort (Nums);
}
}