Java-based binary search algorithm

Source: Internet
Author: User

[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)); }  
}

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.