Java - binary search

Also known as binary search binary search, it is a high efficient search method.

A binary search algorithm is thought by ordering (ascending or descending) arranged in series, the process of finding mode jump find use, i.e., the midpoint position of the first ordered sequence of comparison, if the value is less than the looking element midpoint element sequence reduced to the left half, right half will otherwise unknown origin. By a comparison, look intervals reduced by half. Binary search is an efficient search method. It can significantly reduce the number of comparisons, improve search efficiency. However, a prerequisite is that binary search lookup table data elements must be ordered.

Binary search method advantage is relatively less often find fast, good average performance; disadvantage is the requirement to be ordered lookup table, delete and insert difficult. Therefore, the binary search method is applicable to not change often and find frequently ordered list.

Dichotomy algorithm steps described

① First, determine the entire range to find an intermediate position mid = (left + right) / 2

② key value is compared with a key value to be examined and the intermediate position;

If equal, then find success

If so, then after (right) half region of the binary search continues

If less than 10, the front (left) half region of the binary search continues

③ then reduced area of ​​the determined binary formula, repeat the above steps.

Finally, get results: either to find success, or failure to find. Binary search of a storage structure using a one-dimensional array of storage. Binary search algorithm example

For a given number of columns (ordered) 3,5,11,17,21,23,28,30,32,50,64,78,81,95,101 {}, according to the binary search algorithm, the search key value 81 data elements.

Binary search algorithm discussed:

Advantages: ASL≤log2n, namely through a comparison, it is reduced by half every Look. By log2n time care to complete the discovery process.

Disadvantages: because of the requirements and orderly, it is required to find the number of columns to be ordered, and for all the data elements sorted by size is very time-consuming operation. Further, the structure of the sequential storage insert, delete operation is not convenient.

Consider: Can abandoned by a relatively more parts (ie, after the next comparison, the Look reduced to a smaller), in order to achieve greater efficiency. ......?

Can consider two methods (sequential search and binary search) together, that is to take a simple sequential search and binary search efficiency of the director, to achieve the purpose of improving efficiency? In fact, this idea is the algorithm of block search.

public class BinarySearch {
    public static void main(String[] args) {
        int[] A = { 3,5,11,17,21,23,28,30,32,50,64,78,81,95,101};
        int target = 81;// 查找 81的索引位置;
        int left = 0;
        int right = A.length-1;
        int mid = (left + right) / 2;
        while (left <= right) {
            if (A[mid] == target) {
                System.out.println(mid);
                break;
            }
            if (A[mid] > target) {
                right = mid;
                mid = (left + right) / 2;
            }
            if (A[mid] < target) {
                left = mid;
                mid = (left + right) / 2;
            }
        }
    }
}

  Binary Search Related Items:

private static int binarySearch0(int[] a, int fromIndex, int toIndex,
                                     int key) {
        int low = fromIndex;
        int high = toIndex - 1;

        while (low <= high) {
            int mid = (low + high) >>> 1;
            int midVal = a[mid];

            if (midVal < key)
                low = mid + 1;
            else if (midVal > key)
                high = mid - 1;
            else
                return mid; // key found
        }
        return -(low + 1);  // key not found.
    }

  Excerpt on: https://www.cnblogs.com/wxd0108/p/5465926.html

Guess you like

Origin www.cnblogs.com/strive-19970713/p/11344798.html