Binary Find (Java) (Edge Learning Edge Update)

Source: Internet
Author: User

---restore content starts---

1 classArrayTest32 {3      Public Static voidMain (string[] args)4     {5         //int [] arr=new int[]{54,45,6,5,34,656,34,2};6         //int Index=getindex (arr,2);7         //System.out.println ("index=" +index);8 9         int[] arr1=New int[]{2,4,5,7,12,34,35,46,67,89,99};Ten         intIndex1=halfsearch (arr1,34); OneSystem.out.println ("index1=" +index1); A         intIndex2=halfsearch_2 (arr1,34); -System.out.println ("index2=" +index2); -     } the     /* - binary Find the main idea: - when the value you are looking for (key) is greater than the middle value (Arr[mid]), the subscript (min) of the minimum value becomes the intermediate value subscript plus 1 (mid+1) . - If the value you are looking for is less than the median value, the subscript (max) of the largest value becomes the median subscript minus 1 (mid-1). + Returns the mid-value subscript after the lookup, otherwise returns-1.  -     */ +     //binary Lookup is more efficient, but only if the array must be ordered A      Public Static intHalfsearch (int[] arr,intkey) at     { -         intMin=0,max=arr.length-1, mid; -          -          while(Min<max)//Jumps out of the loop when the subscript of the smallest value is greater than the maximum value, indicating that the specified value is not found -         { -Mid= (Min+max)/2; in             if(key>Arr[mid]) -Min=mid+1; to             Else if(key<Arr[mid]) +Max=mid-1; -             Else  the                 returnmid; *         } $         return-1;Panax Notoginseng     } -  the     //Another kind of binary sort of idea +      Public Static intHalfsearch_2 (int[] arr,intkey) A     { the         intMin=0,max=arr.length-1, mid; +Mid= (Min+max)/2; -          while(Arr[mid]!=key)//The value is not equal to the value specified, which means that the value cannot be found (not present) $         { $Mid= (Min+max)/2; -             if(key>Arr[mid]) -Min=mid+1; the             Else if(key<Arr[mid]) -Max=mid-1;Wuyi             if(min>max) the                 return-1; -Mid= (Min+max)/2; Wu         } -             returnmid; About     } $  -  -  -     //find the position where the array value first appears for the key A      Public Static intGetIndex (intarr [],intkey) +     { the          for(inti=0;i<arr.length;i++) -         { $             if(arr[i]==key) the                 returni; the         } the         return-1; the     } -}

---restore content ends---

Binary Find (Java) (Edge Learning Edge Update)

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.