Java implementation of the dichotomy method

Source: Internet
Author: User

Http://www.cnblogs.com/vanezkw/archive/2012/06/29/2569470.html

implementation of the binary method inside the JDK. 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 search.
* @autor Kwzhang
* modify:2012-6-29
*
* @param <E>
* @param array to find in array
* @param from start subscript
* @param to stop subscript
* @param key to find keywords
* @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); Move right to remove 2
E temp = 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.
}
 RELATED Links:comparison of dichotomy and bubbling method (fast sorting method)The binary method of data structure and the idea and realization of fast sortingDichotomy -Baidu EncyclopediaBaidu Search Two-part method 

Java implementation of the dichotomy method

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.