Binary Insertion Sort Java implementation

Source: Internet
Author: User

 PackageKpp.sort;/*** Currently to be inserted element data[i], if DATA[I]>=DATA[I-1], then the order is normal, i++ processing the next element * if DATA[I]<DATA[I-1], first save data[i] to temp, Find a suitable insertion position k, from K to i-1 element order right * Insert temp into k * * Analysis: * Binary insertion sorting is independent of the initial state of the record to be sorted and depends only on the number of records. * When n is larger, the maximum number of comparisons is much less than the direct insert sort. But the minimum number of comparisons is greater than the direct insertion sort. * The number of moves of the algorithm is the same as the direct insertion sorting algorithm, the worst case is N2/2, the best case is N, and the average number of moves is O (N2). *  * @authorKPP **/ Public classMiddleinsertsort { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        intArray[] = {49,38,65,97,176,213,227,49,78,34,12,164,11,18,1};        Midinsertsort (array);  for(intK:array)        {System.out.println (k); }    }    Private Static intMidinsertsort (inta[]) {        intLen =a.length;  for(inti = 1;i < len;i++){            //because the sequence preceding the insertion sort is ordered, the current element is judged to be smaller than the previous element,//if small, do a binary insert sort, or if large, indicates that the current sort is correct and the next element is processed            if(A[i] < a[i-1]){                //Save the value with insert first .                inttemp =A[i]; //find where to insert in two points                intleft = 0; intright = I-1; intMID = 0;  while(Left <=Right ) {Mid= (right+left)/2; if(temp = =A[mid]) { Left=mid;  Break; }                     Else if(Temp <A[mid]) { Right= Mid-1; }Else{ Left= Mid + 1; }                }                //The position to be inserted is left, and the element is moved right                 for(intj = I-1;j >= left;j--) {a[j+1]=A[j]; } A[left]=temp; }                    }        return0; }}

Binary Insertion Sort Java implementation

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.