[What is the two-point search]
Binary search, also known as binary lookup, the idea of the algorithm is to arrange the sequence of sequences, using a jump-type method to find, that is, first of all ordered sequence of the midpoint position as the comparison object,
If the element value you are looking for is less than the midpoint element, the sequence of the order is reduced to the left half, otherwise the right half. And so on, narrowing the search scope.
[Condition of binary search]
A prerequisite for a binary lookup is that the sequence of lookups must be ordered.
[The advantages and disadvantages of the two-point search]
Advantages: Less times, faster search speed, good average performance;
Disadvantages: Requiring the number of the numbers to be ordered, and inserting and deleting difficulties;
Applicable scenario: Find frequent ordered lists without frequent changes.
[Algorithm Step description]
① first determines the middle position of the entire lookup interval mid = (min + max)/2
② compares the key value of the estimate with the value of the middle position:
I. If the equality, then find success;
II. If less than, the first half of the region to continue to binary search;
III. If greater than, then continue to binary in the latter half of the region;
③ the defined narrowing area and then press the binary formula to repeat the above steps.
④ finally got the result: either look for success or find failure. The storage structure of the binary lookup is stored using one-dimensional arrays.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/
[Java Implementation]
public class BinarySearch {public static int binarysearch (int[] arr, int target) {if (arr!= null) {
int min, Mid, Max; min = 0; The minimum index max = arr.length-1;
The maximum index while (min <= max) {mid = (min + max)/2;//The Middle index
if (Arr[mid] < target) {min = mid + 1;
else if (Arr[mid] > target) {max = mid-1;
else {return mid;
}} return-1; }//test case public static void main (string[] args) {int[] arr = {1, 2, 3, 5, 6, 7, 8
, 9, 23, 34};
System.out.println (BinarySearch (arr, 5));
System.out.println (BinarySearch (arr, 23));
System.out.println (BinarySearch (arr, 0)); System.out.println (BinarySearch (ARR, 35)); }
}