Java implementation of binary (binary) Insert sort

Source: Internet
Author: User

With a sequence a[0],a[1]...a[n], where a[i-1] is already ordered, when inserting a[i], the binary search a[i] Insert position

Efficiency: O (n^2), for the initial basic ordered sequence, the efficiency is inferior to the direct insertion sort; for random unordered sequences, the efficiency is higher than the direct insertion sort.

/* * BINARY (binary) Insert sort * with a sequence a[0],a[1]...a[n], where a[i-1] is already ordered, when inserting a[i], use dichotomy to search A[i] Insert position */public class Binaryinsertsort { public static void Main (string[] args) {int len = 10;int[] ary = new Int[len]; Random random = new random (), for (int j = 0; J < Len; J + +) {Ary[j] = random.nextint (1000);} Binaryinsert (ary);/* Complexity analysis: the best case, that is, the order, there is no need to move right, the time complexity is: O (n lg N) The worst case, all in reverse order, when the complexity of O (n^2) * Can not increase the complexity of the worst case to O (N|logn). *///Print array printarray (ary);} /** * Insert Sort * @param ary */private static void Binaryinsert (int[] ary) {int setvaluecount = 0;//sort from the second element of the array, because the first element itself must have been Ordered for (int j = 1; j < Ary.length; J + +) {//complexity n//Save current value int key = ary[j];//? Use binary lookup to position the insertion position//int index = BinarySearch ASC (ary, Ary[j], 0, j-1);//complexity: O (logn)//int index = Binarysearchdesc (ary, Ary[j], 0, j-1);//complexity: O (logn) int index = b INARYSEARCHDESC2 (ary, Ary[j], 0, j-1);//Complexity: O (LOGN) printArray (ary); System.out.println (the position of the element on the "+ j +" Index to be inserted is: "+ index);//The target is inserted in the position, and the right side of the target position is moved to the right of the element for (int i = j; i > Index; i--) {//complexity, Worst Case: (n1) + (n-2) +...+n/2=o (n^2) ary[i] = ary[i-1]; I-1 <==> indexsetvaluecount++;} Ary[index] = key;setvaluecount++;} System.out.println ("\ n Set the number of values (Setvaluecount) =====>" + Setvaluecount);} /** * Binary Search Ascending recursion * * @param ary * Given sorted unknown origin array * @param target * Find target * @param from * current find The range starting point * @param to * The return end point of the current lookup * @return returns the destination in the array, in order where it should be */private static int binarysearchasc (int[] ary, int t arget, int from, int to) {int range = to-from;//If the range is greater than 0, that is, there are more than two elements, continue to split if (range > 0) {//select intermediate bit int mid = (to + from )/2;//If the critical bit is not met, then continue to find if (Ary[mid] > target) {/* * mid > Target, ascending rule, target smaller, should be swapped position in front, that is, target is positioned in the mid position, * According to the check Find ideas, from the from to the mid-1 think orderly, so to=mid-1 */return binarysearchasc (ary, Target, from, mid-1);} else {/* * Mid < Target, ascending rule, target larger, no swap position, lookup comparison starting position should be mid+1 */return binarysearchasc (ary, Target, mid + 1, to);}} else {if (Ary[from] > target) {//as 5,4, to insert 4return from;} else {return from + 1;}}} /** * Binary search Descending, recursive */private static int Binarysearchdesc (int[] ary, int target, int from, int to) {int range = to-from;if (Range > 0) {int mid = (from + to) >>> 1;if (Ary[mid] > target) {return Binarysearchdesc (ary, Target, mid + 1, to);} else {return bin Arysearchdesc (ary, Target, from, mid-1);}} else {if (Ary[from] > target) {//As 5,4, 4return from + 1 is to be inserted;} else {return from;}}} /** * Binary Lookup Descending, non-recursive */private static int binarySearchDesc2 (int[] ary, int target, int from, int. to) {//while (from < to) {for (; from < to;) {int mid = (from + to) >>> 1;if (Ary[mid] > target) {from = Mid + 1,} else {to = mid-1;}} From <==> to;if (Ary[from] > target) {//5,4, to insert 4return from + 1;} else {return from;}} private static void PrintArray (int[] ary) {for (int i:ary) {System.out.print (i + "");}}}
Print


Java implementation of binary (binary) Insert 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.