Java binary search and java Binary Search

Source: Internet
Author: User

Java binary search and java Binary Search

Binary Search, also known as semi-query, is an efficient search method.

The algorithm of semi-query is to sort the series in order (ascending or descending). During the search process, the Skip method is used to search, that is, the point position of the ordered series is used as the comparison object first, if the value of the element to be searched is smaller than the midpoint element, the sequence to be queried is reduced to the left half, otherwise it is the right half. After a comparison, the search interval is halved. Semi-query is an efficient search method. It can significantly reduce the number of comparisons and improve the search efficiency. However, a prerequisite for semi-query is that the data elements in the table must be sorted.

The advantage of the semi-query method is that the query speed is fast and the average performance is good. The disadvantage is that the table to be queried must be an ordered table and it is difficult to insert or delete the table. Therefore, the half-fold lookup method is suitable for searching frequently ordered lists without frequent changes.

Process description of the binary algorithm:

1. Determine the middle position of the entire search interval mid = (left + right)/2

2. Use the keyword value to be queried to compare with the keyword value at the intermediate position;

If they are equal, the search is successful.

If the value is greater than, the half-fold search will continue in the last (right) Half area.

If the value is smaller than the value, the half search will continue in the first (left) Half area.

3. Repeat the preceding steps based on the half-fold formula for the area to be reduced.

4. Expected results: either the search is successful or the search fails. The storage structure of semi-query is stored in a one-dimensional array. Example of a half-Lookup Algorithm

Note: The search fails (that is, the search content does not contain what you are looking for), and no error is reported on the console.

Code example:

Package com. shuzu; public class Demo7 {public static void main (String [] args) {// TODO Auto-generated method stub // test, array (ordered) int arr [] = {1, 3, 4, 7, 11, 16, 33, 43, 65, 77, 88, 90, 99,110 }; binaryFind bf = new BinaryFind (); bf. find (0, arr. length-1, 33, arr) ;}// defines a Binary Search class BinaryFind {public void find (int leftIndex, int rightIndex, int val, int arr []) {// first, find the number in the middle. Do not worry about it here. In Java, the entire int midIndex = (rightIndex + leftIndex)/2 is automatically obtained except for all; int midVal = arr [midIndex]; if (rightIndex> = leftIndex) {// if the number to be searched is greater than midVal if (midVal> val) {// find (leftIndex, midIndex-1, val, arr) on the left of the arr array );} else if (midVal <val) {// find (midIndex + 1, rightIndex, val, arr) on the right of the arr array;} else if (midVal = val) {System. out. println ("" + midIndex );}}}}

Example principle:

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.