This section introduces a Insertion Method missing during insertion sorting, that is, semi-insertion. This method uses the binary search method to find the insert point, which can reduce the number of element comparisons, but cannot reduce the number of moves. The complexity is the same as that of direct insertion, and all values are O (N ^ 2 ).
Directly run the Code:
// Binary insert sorting void binary_insert_sort (INT arr [], int Len) {If (ARR = NULL | Len <= 1) {return;} int I, J; for (I = 1; I <Len; I ++) {int low = 0, high = I-1; int target = arr [I]; while (low <= high) {int middle = low + (high-low)> 1); If (ARR [Middle]> Target) {high = middle-1 ;} the position of else {LOW = middle + 1 ;}// low is exactly the insert point. // The elements between I and low are moved one by one (excluding I) for (j = I; j> low; j --) {arr [J] = arr [J-1];} // Insert the target to the correct position. Arr [low] = target ;}}