--java implementation of binary insertion sorting algorithm for sorting series

Source: Internet
Author: User

Basic idea:

The binary insertion algorithm is an improvement to the direct insertion sorting algorithm, with the sorting principle and the direct insertion algorithm:

The n order of the elements to be sorted as an ordered table and an unordered table, the beginning of the ordered table has only one element, the unordered table has n-1 elements; The sorting process takes the first element out of an unordered table, inserts it into an ordered table, makes it a new ordered table, and repeats the entire sort process n-1 times.

The difference from the direct insertion algorithm is that the binary lookup/Two-point lookup is used when looking for the correct position of the sorted data in an ordered table.

 Instance:

(Refer to direct insertion sorting algorithm: http://www.cnblogs.com/snowcan/p/6244128.html)

Code that differs from the Direct insertion algorithm (binary lookup):

        /*** Find the correct location for temp insertion in the sequence list, using the binary lookup method*/         while(Low <=High ) {            /*** Intermediate coordinates of an ordered array, which is used for two-point lookups, reducing the number of lookups*/            intMid = (Low+high)/2; /*** If the intermediate element of an ordered array is greater than the element to be sorted, the ordered sequence is searched before the intermediate element, otherwise it is searched backwards*/            if(a[mid]>temp) { High= Mid-1; }Else{ Low= Mid+1; }        }                    

Java implementations:

 Packagesort;/*** Binary Insertion Sort Implementation * Stabilization algorithm *@authorThe ginkgo leaves of the season*/ Public classInsertsort { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        intA[] = {3,1,5,7,2,4,9,6}; NewInsertsort (). Binaryinsertsort (a); }        /*** Implementation of binary insertion sorting algorithm *@parama*/    Private voidBinaryinsertsort (int[] a) {//TODO auto-generated Method StubSystem.out.println ("——————————————————— binary insertion sorting algorithm —————————————————————"); intn =a.length; inti,j;  for(i=1;i<n;i++){            /*** Temp is the number in the sequence table to be inserted for this cycle*/            inttemp =A[i]; intLow=0; intHigh=i-1; /*** Find the correct location for temp insertion in the sequence list, using the binary lookup method*/             while(Low <=High ) {                /*** Intermediate coordinates of an ordered array, which is used for two-point lookups, reducing the number of lookups*/                intMid = (Low+high)/2; /*** If the intermediate element of an ordered array is greater than the element to be sorted, the ordered sequence is searched before the intermediate element, otherwise it is searched backwards*/                if(a[mid]>temp) { High= Mid-1; }Else{ Low= Mid+1; }            }                         for(j=i-1;j>=low;j--){                /*** element is moved back, ready to insert temp*/a[j+1] =A[j]; }            /*** Insert Temp*/A[low]=temp; /*** Print the results of each cycle*/print (a,n,i); }        /*** Print Sort results*/Printresult (a,n); }    /*** Print the final result of sorting *@paramA *@paramN*/    Private voidPrintresult (int[] A,intN) {System.out.print ("Final sort Result:");  for(intj=0;j<n;j++) {System.out.print (" "+A[j]);    } System.out.println (); }    /*** Print the result of each cycle of sorting *@paramA *@paramN *@paramI*/    Private voidPrintint[] A,intNinti) {//TODO auto-generated Method StubSystem.out.print ("+i+" Times: ");  for(intj=0;j<n;j++) {System.out.print (" "+A[j]);    } System.out.println (); }}

Running results show:

 (This article is only for learning exchange, if there are better ideas, welcome to leave comments for everyone to explore learning ~)

--java implementation of binary insertion sorting algorithm for sorting series

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.