Java Sort algorithm--two-point insertion sort __ encoding

Source: Internet
Author: User

Binary Insertion Sort
Rationale: For an improvement to a direct insertion sort, you can quickly determine the insertion position of the first element by continually binary. That is, the middle point of the 0~i-1 is computed, and the elements at the I index are compared with the median. If the element at the I index is large, it means that the element to be inserted should be on the right side of the middle value, whereas on the left (small to large)
time efficiency: O (n2) O (n^2)
Space efficiency: O (1) O (1)
Algorithm Stability: unstable

Binary insertion sort (small to large sort) public class Sort_binary {private void Binarysort (int nums[]) {int length = Nums.length;
        int minindex = 0; for (int i = 1;i<length;i++) {//start with the second element int left=0;//left pointer int right=i-1;//right pointer int tem p = nums[i];//the element to which the loop is to be inserted//To determine the position index while (left<=right) {Minindex = (left+rig
                HT)/2;
                if (Nums[minindex]<nums[i]) {left = minindex+1;
                }else{right = minIndex-1; }//move the original array element after the insertion position index for (int k =i;k>left;k--) {nums[k] = Nums[k-1
            ];
        }//Update element Nums[left for insert location index = temp;
    } System.out.println (Java.util.Arrays.toString (nums));
        }//test function public static void main (string[] args) {int nums[]={89,54,78,8,398,7,560,25,963,16};
   Sort_binary test = new Sort_binary ();     Test.binarysort (Nums);
 }
}

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.