Java implementation of binary search algorithm

Source: Internet
Author: User

Today I looked at the binary method inside the JDK is realized, feel a little problem. The realization of the dichotomy has a variety of today to share two kinds. One is recursive, and one is a non-recursive method. Let's take a look at some basic things first.

1, algorithm concept.

The binary lookup algorithm, also known as binary search and binary search, is a search algorithm for finding a particular element in an ordered array. Please note that this algorithm is based on an ordered array.

2, the algorithm idea.

① The search process starts from the middle element of the array, and if the intermediate element is exactly the element to be found, the search process ends;

② if a particular element is greater than or less than the middle element, it is found in the half of the array greater than or less than the middle element, and is compared with the beginning from the middle element.

③ if an array of steps is empty, the representation cannot be found.

Each comparison of this search algorithm reduces the search scope by half.

3, realize the idea.

① finds the value in the middle of the array and stores it in a variable (for the following description, the variable is temporarily named temp);

② need to find the key and temp to compare;

③ if the key value is greater than temp, the middle position of the array is used as the starting point for the next calculation; repeat ①②.

④ if the key value is less than temp, the middle position of the array is used as the end point for the next calculation; repeat ①②③.

⑤ if the key value equals temp, the array subscript is returned to complete the lookup.

4, implementation code.

/** * Description: Two-point lookup.     * @autor Kwzhang * modify:2012-6-29 * * @param <E> * @param array needs to find an ordered array * @param from start subscript  * @param to stop subscript * @param key keyword to find * @return * @throws Exception */public static <e extends  comparable<e>> int BinarySearch (e[] array, int from, int to, E key) throws Exception {if (from < 0 | |        to < 0) {throw new IllegalArgumentException ("params from & length must larger than 0."); if (from <= to) {int middle = (from >>> 1) + (to >>> 1);//2 E T with right shift            EMP = Array[middle];            if (Temp.compareto (key) > 0) {to = middle-1;            } else if (Temp.compareto (key) < 0) {from = middle + 1;            } else {return middle;    }} return BinarySearch (array, from, to, key); }

5, test demo is very simple, here will not write.

The above implementation is recursive, and everyone feels that such problems are better understood by recursion, and the process is simple.

Let's look at how the non-recursive approach is implemented. In the JDK there is a realization, directly affixed to the code inside the arrays. For the sake of simplicity, let's just look at the method of the int parameter:

  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.    }  

The above method is I directly in the SDK inside copy (I have not changed). This approach is emphasized because the method personally feels that there is a problem (I believe you will feel it too).

When the key is the last value in the array a, then it will not be found (don't understand what sun was thinking). Personally feel the need to put int high = toIndex-1; change to int high = Toindex;

This algorithm ends here. Welcome to discuss communication.

Java implementation of binary search algorithm

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.