Recursive binary lookup initial start=0, end = array.length-1
static int binarysearch4recursion (int[) array, int data, int start, int end {
int mid =-1;
System.out.println ("Find Times");
if (Start > End) {return
mid;
}
Mid = (start + end) >>> 1;
if (Array[mid] < data) {return
binarysearch4recursion (array, data, Mid + 1, end);
} else if (Array[mid] > Data) {return
binarysearch4recursion (array, data, start, mid-1), or
else {return
mid;
}
Binary insertion Sequencing
There is a sequence a[0],a[1]...a[n] in which a[i-1] is already ordered, and when inserted a[i], a binary search a[i the inserted position
Efficiency: O (n^2), for the initial basic ordered sequence, less efficient than the direct insertion order; For random unordered sequences, the efficiency is higher than the direct insertion sort.
* * * BINARY (binary) insert ordering * with a sequence a[0],a[1]...a[n] in which a[i-1] is already ordered, when inserted a[i], use the binary search A[i] Insert Position * * * public class Binaryinsertso
RT {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: In the best case, which is already sorted, there is no need to move right, at a time when the complexity is: O (n lg N) worst case, all in reverse order, at which point the complexity O (n^2) * Cannot elevate 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 is definitely a for (int j = 1; j < Ary.length; J +) {//complexity N//save current value int key = Ary[j]; ∆ uses the binary lookup to locate the insertion position//INT index = BINARYSEARCHASC (ary, Ary[j], 0, j-1);//Complexity: O (LOGN)//INT index = Binarysea
Rchdesc (ary, Ary[j], 0, j-1);//complexity: O (logn) int index = BINARYSEARCHDESC2 (ary, Ary[j], 0, j-1);//Complexity: O (LOGN) PrintArray (ary);
System.out.println ("" "+ j +" index elements to insert the position is: "+ index); Inserts the target to the right of the element for (int i = j; i > Index; i--) {//complexity, Worst case: (n-1) + (n-2) +...+n/2=o (n^2) ary[i] = ary[ I-1];
I-1 <==> index setvaluecount++;
} Ary[index] = key;
setvaluecount++;
} System.out.println ("\ n Set value number (Setvaluecount) =====>" + setvaluecount); /** * Binary Lookup Ascending recursive * * @param ary * Given the sorted array of orders of @param target * Find target * @param from * current The range starting point of the lookup * @param to * The return endpoint * of the current lookup * @return returns the destination in the array, in the order in which it should be in the position */private static int binarysearchasc (int[] ar
Y, int target, int from, int to) {int range = To-from;
If the range is greater than 0, that is, there are more than two elements, then continue splitting if (range > 0) {//select Intermediate int mid = (to + from)/2; If the critical bit is not satisfied, continue with the binary lookup if (Ary[mid] > target) {/* mid > Target, ascending rule, target smaller, swap position front, i.e. target positioned in mid Position, * According to the search idea, from the from to 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, the starting position for the lookup comparison should be mid+1/return binarysearchasc (ary, T
Arget, Mid + 1, to);
} else {if (Ary[from] > target) {//As 5,4, to be inserted is 4 return from;
else {return from + 1;
/** * Binary Lookup 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 Binarysearchdesc (ary, Target, from, mid-1);
} else {if (Ary[from] > target) {//5,4, to be inserted is 4 return from + 1;
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) {//As 5,4, to be inserted is 4 return from + 1;
else {return from;
} private static void PrintArray (int[] ary) {for (int i:ary) {System.out.print (i + "");
}
}
}
Print
918 562 442 531 210 216 931 706 333 132 The elements on the 1th index are to be inserted at the position of: 1 918 562 442 531 210 216 931 706 333 132
2nd The elements on the index to be inserted are: 2< c2/>918 562 442 531 210 216 931 706 333 132 The elements on the 3rd index are to be inserted at the position of: 2 918 562 531 442 210 216 931 706 333 132
The 4th index elements to be inserted is: 4 918 562 531 442 210 216 931 706 333 The elements on the 5th index to be inserted are: 132 4 918 562 531 442 216 210 931 706 333
6th index elements to insert The position is: 0 931 918 562 531 442 216 210 706 333 The
elements on the 7th index to insert are: 132 2 931 918 706 562 531 442 216 210 333
8th Index The element to be inserted in is: 6 931 918 706 562 531 442 333 216 210
The elements on the 9th index are to be inserted at the position: 132
Set value number (Setvaluecount) =====> 24
931 918 706 562 531 442 333 216 210 132