Binary Insert sort is an improvement on the direct insertion sort, which improves the query efficiency by using the binary search algorithm when looking for the insertion point.
Sort Ascending:
/** * Binary Insert sort * ascending sort * * * @Overridepublic <t extends comparable<? Super t>> void Sortbyasc (t[] data) {for (int i = 1;i < data.length;i++) {if (Data[i].compareto (data[i-1]) < 0) {/** record I value */t temp = data[i];/** records the left edge of the search range */int low = 0;/** records the right border of the search range */int high = i-1;while (Low <= high) {/** record middle position */in T mid = (high + low)/2;/** compares intermediate position data and I data size to narrow the search */if (Data[mid].compareto (temp) < 0) {low = mid + 1;} Else{high = Mid-1;}} /** move the data low~i the whole backward movement */for (int j = i; j > low; j--) {data[j] = data[j-1];} Data[low] = temp;}}}
Sort Descending:
/** * Binary Insert Sort * descending sort * * @Overridepublic <t extends comparable<? Super t>> void Sortbydesc (t[] data) {for (int i = 1;i < data.length;i++) {if (Data[i].compareto (data[i-1]) > 0 {/** records the value of i */t temp = data[i];/** records the left edge of the search range */int low = 0;/** records the right border of the search range */int high = i-1;while (Low <= high) {/** record middle position */i NT MID = (high + low)/2;/** compares intermediate position data and I data size to narrow the search */if (Data[mid].compareto (temp) > 0) {low = mid + 1;} Else{high = Mid-1;}} /** move the data low~i the whole backward movement */for (int j = i; j > low; j--) {data[j] = data[j-1];} Data[low] = temp;}}}
The sorting algorithm of data structure Java implementation (6)--Binary insertion sorting algorithm for inserting class sort