Java Binary Search method

Source: Internet
Author: User
public class Querydemo {public static void main (string[] args) {int[] arr2 = new int[] {11, 3,32,34, 45, 56, 78}; System.out.println ("67 in the array position:" + halfsearch (ARR2, 11));} public static int Halfsearch (int[] arr, int value) {//define 3 variables min, Mid, maxint min = 0;int max = Arr.length-1;int mid = (min + max)/2;while (Value! = Arr[mid]) {//Determine if (Value > Arr[mid]) {//on right min = mid + 1;} else {//on Left max = mid-1; }//Update Midmid = (min + max)/2;//The proof data is not queried to if (Min > Max) {mid = -1;break;}} When the minimum angle is greater than the maximum angle index return mid;}}

Java Binary Search 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.