Binary is generally used between a[k] and A[r]. When the middle node is A[K+R/2]. After a comparison can be ruled out half of the record, the possible insertion of the interval reduced by half, so called binary. The prerequisite for running the binary insertion sort is that the file records must be stored sequentially.
(3) After the position is determined. Moves the entire sequence back and inserts the element into the corresponding position.
Third, the code implementation
public class Binarysort {public static void Binarysort (int[] source) {int I, j;int high, Low, mid;int temp;for (i = 1; I & Lt Source.length; i++) {//Lookup area upper Bound low = 0;//Find area lower bound high = i-1;//Save the currently pending record in a temporary variable temp = source[i];while (Low <= High) {//Find middle value/mid = (lo W + high)/2;mid = (low + high) >> 1;//assume that the record to be inserted is less than the intermediate record if (Temp<source[mid]) {//the insertion point is in the lower half of the zone high = mid-1;} else {// Insertion point in high half area low = mid + 1;}} Move all previous records that are larger than the current record to be inserted for (j = i-1; J >=low; j--) {source[j + 1] = source[j];} Backfill the record to be inserted in the correct position. Source[low] = temp; System.out.print ("First" + i + "sequencing:");p Rintarray (source);}} private static void PrintArray (int[] source) {for (int i = 0; i < source.length; i++) {System.out.print ("\ t" + source[i ]);} System.out.println ();} public static void Main (string[] args) {int source[] = new int[] {12, 15, 9, 14, 4, 18, 23, 6}; System.out.print ("Initial keyword:");p Rintarray (source); System.out.println (""); Binarysort (source); System.out.print ("\ n-sort result:");p Rintarray (source);}}Iv. Results of implementation:
Initial keyword:1215914418236 1th trip sort: 1215914418236 the first 2 orders: 9121514418236 The first 3 orders: 9121415418236 the 4 order: 4912141518236 The 5 order: 4912141518236 the 6 times Order: 4912141518236 The first 7 orders: 4691214151823 sorted Results: 4691214151823
==================================================================================================
Ouyangpeng welcome reprint, sharing with people is the source of progress!
Reprint please keep the original address : Http://blog.csdn.net/ouyang_peng
==================================================================================================
Copyright notice: This article Ouyangpeng original article, welcome reprint, Reprint please indicate the source Http://blog.csdn.net/ouyang_peng
Mine Java Development Learning tour------> Java Sorting algorithm classic binary insertion sort