Mine Java Development Learning tour------> Java Sorting algorithm classic binary insertion sort

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, is when inserting a[i], take A[I-1/2] key code value and A[i] of the key code value, assuming a[i] key code value is less than A[I-1/2] key code value. A[i] can only insert a[0] to A[I-1/2]. It is possible to continue to use binary between a[0] to a[i-1/2-1], otherwise it can only be inserted between A[I-1/2] to a[i-1]. It is possible to continue to use binary between a[i-1/2+1] to a[i-1]. This will be done until the position of insertion is determined at the end.

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.

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. The elements at the I index are compared with the median values. Assume that the element at the I index is large, indicating that the element to be inserted should be between the middle value and the I index just added. Instead. is at the beginning of the position to the median position, so very easy to complete the binary;(2) in the corresponding half of the range to find the insertion position. Keep using (1) steps to narrow down the range. Keep on binary. The range is reduced in turn to 1/2 1/4 1/8 .... The high-speed determines where the first element is to be inserted. (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------&gt; Java Sorting algorithm classic binary insertion sort

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.