The dichotomy of Java

Source: Internet
Author: User

  1. Don't say much nonsense, first come to the program. Step by step look at the notes,

  2. The first thing to note is that when using the dichotomy, the array must be ordered, that is, from large to small, or from smallest to largest, and cannot be unordered.

    1  Public Static intHalfsearch (int[] arr,intkey) {2             intMin,max,mid;//minimum, maximum, and median values, respectively3Min = 0;4max = Arr.length-1;5Mid = (max + min)/2;//This should be the core of the two points,6             //when key = Arr[mid] indicates that the data has been found, so the condition of the while loop is! =7              while(Arr[mid]! =key) {8                 if(Key >Arr[mid]) {9Min = mid + 1;Ten}Else if(Key <Arr[mid]) { Onemax = Mid-1; A                 } -                 //Returns-1 indicates no found -                 if(Max <min) { the                     return-1; -                 } -Mid = (max + min)/2; -             } +             //The return value for the corresponding key is worth subscript -             returnmid; +}

The dichotomy of 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.