Binary lookup Algorithm (JAVA)

Source: Internet
Author: User

1. Two points lookup is also called binary lookup, it is a more efficient way to find.

2. Two-point lookup requirements: (1) The sequential storage structure (2) must be used. Must be sorted by keyword size

3. Principle: The array is divided into three parts, followed by the median (the so-called median is the middle of the array of values) before, median, median, the value to be looked up and the median value of the array, if less than the median value in front of the median value is found, if the value is greater than the median value is returned after the median Then there is a recursive procedure, in which the first half or the second part continues to be decomposed into three parts.

4. Implementation: Binary lookup implementation by recursion and loop two ways

5. Code:

1  PackageOther ;2 3  Public classBinarySearch {4     /*5 * Loop Implementation binary Lookup algorithm arr already ordered array x number of lookups required-1 Unable to find data6      */7      Public Static intBinarySearch (int[] arr,intx) {8         intLow = 0; 9         intHigh = Arr.length-1; Ten          while(Low <=High ) {    One             intMiddle = (low + high)/2;  A             if(x = =Arr[middle]) {    -                 returnMiddle;  -}Else if(X <Arr[middle]) {    theHigh = Middle-1;  -}Else {    -Low = middle + 1;  -             }   +         }   -         return-1;  +     } ARecursive implementation of binary search at      Public Static intBinarySearch (int[] DataSet,intDataintBeginindex,intEndIndex) {     -            intMidindex = (beginindex+endindex)/2;  -            if(Data <dataset[beginindex]| | data>dataset[endindex]| | Beginindex>EndIndex) {   -                return-1;  -            }   -            if(Data <Dataset[midindex]) {     in                returnBinarySearch (dataset,data,beginindex,midindex-1);  -}Else if(data>Dataset[midindex]) {     to                returnBinarySearch (dataset,data,midindex+1, EndIndex);  +}Else {     -                returnMidindex;  the            }     *        }    $ Panax Notoginseng      Public Static voidMain (string[] args) { -         int[] arr = {6, 12, 33, 87, 90, 97, 108, 561 }; theSystem.out.println ("Loop lookup:" + (BinarySearch (arr, 87) + 1)); +SYSTEM.OUT.PRINTLN ("Recursive lookup" +binarysearch (arr,3,87,arr.length-1)); A     } the}

Binary lookup Algorithm (JAVA)

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.