My Java Development Learning journey------Binary Insertion sort of >java classic sorting algorithm

Source: Internet
Author: User


First, binary Insert Sort ( Two-point insertion sort )

the method of inserting the insertion position in the direct insertion sort to find a[i] is replaced by the binary comparison, and the binary insertion sorting algorithm can be obtained. When dealing with a[i], a[0] ... A[I-1] has been ordered by key code values. The so-called binary comparison, is when inserting a[i], take A[I-1/2] key code value and A[i] key code value, if A[I] key code value is less than A[I-1/2] key code value, then a[i] can only insert a[0] to A[I-1/2] between, it can be in a[0] Continue to use binary comparisons between a[i-1/2-1], otherwise you can only insert A[I-1/2] to a[i-1], so a[i-1/2+1 comparisons may continue between a[i-1] to binary. This will be done until the position of insertion is determined at the end. Generally in a[k] and a[r] between the use of binary, wherein the 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 performing a binary insert sort is that the file records must be stored sequentially.

Second, the principle of the algorithm

binary algorithm idea for inserting sorting:the basic process of the algorithm:(1) Calculate the middle point of 0 ~ i-1, with the element at the I index and the median value, if the element at the I index is large, indicating that the element to be inserted between the middle value and just added I index, and vice versa, is at the beginning of the position to the median position, so it is very simple to complete the binary;(2) in the corresponding half of the area to find the insertion position, the continuous use of (1) steps to narrow the range, non-stop binary, the range is reduced to 1/2 1/4 1/8 .... Quickly determine where the first element is to be inserted;(3) After the position is determined, the entire sequence is moved back and the element is inserted into the appropriate 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;//Lookup area lower bound high = i-1;//Save the currently pending record in temporary variable temp = source[i];while (Low <= High) {//Find middle value/mid = (lo W + high)/2;mid = (low + high) >> 1;//if 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 Operation:
Initial keyword: 1215914418236 the first 1 orders: 1215914418236 The first 2 order: 9121514418236 the 3 order: 9121415418236 the 4 order: 4912141518236 The 5 order: 4912141518236 The first 6 orders: 4 912141518236 Part 7 order: 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

==================================================================================================




My Java Development Learning journey------Binary Insertion sort of >java classic sorting algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.