# Binary search Algorithm Java

Source: Internet
Author: User

Binary search, also known as binary lookup, is a highly efficient method of finding.

Binary find the algorithm idea is to order the sequence (increment or decrement) arrangement, the search process using a jumping way to find, that is, the midpoint position of the ordered series is the comparison object, if the element value to find is less than the midpoint element, then the unknown origin sequence is reduced to the left half, otherwise the right half part. Reduce the lookup interval by half by a single comparison. Binary lookup is an efficient way to find. It can significantly reduce the number of comparisons and improve search efficiency. However, the prerequisite for binary lookup is that the data elements in the lookup table must be ordered.

The advantage of binary lookup method is that the number of comparisons is small, the searching speed is fast, the average performance is good, the disadvantage is that the unknown Origin table is ordered and the insertion and deletion is difficult. Therefore, the binary lookup method is suitable for an ordered list that does not change frequently and finds frequent.

Algorithm Step Description

① first determines the middle position of the entire search interval mid = (left + right)/2

② is compared with the key value of the middle position with the unknown origin keyword value;

If equal, the lookup succeeds

If it is greater then the binary lookup is continued in the back (right) half of the area

If it is less than, continue to binary lookup in the front (left) half of the area

③ repeat the above steps by pressing the binary formula on the defined narrowing area.

Finally, you get the result: either the lookup succeeds or the lookup fails. The storage structure found by binary is stored in a one-dimensional array.

` PackageSrc.com.sunchis.basic;  Public classBinarySearch {/*** Binary Search algorithm * *@paramSrcarray ordered array *@paramKey Find element *@returnarray subscript for key, not found return-1*/       Public Static voidMain (string[] args) {intSrcarray[] = {3,5,11,17,21,23,28,30,32,50,64,78,81,95,101}; System.out.println (Binsearch (Srcarray,0, Srcarray.length-1, 81)); }          //recursive implementation of binary search     Public Static intBinsearch (intSrcarray[],intStartintEndintkey) {           intMid = (End-start)/2 +start; if(Srcarray[mid] = =key) {               returnmid; }           if(Start >=end) {               return-1; } Else if(Key >Srcarray[mid]) {               returnBinsearch (Srcarray, Mid + 1, end, key); } Else if(Key <Srcarray[mid]) {               returnBinsearch (Srcarray, start, mid-1, key); }           return-1; }          //Two-point lookup for Common Loop implementations     Public Static intBinsearch (intSrcarray[],intkey) {           intMID = Srcarray.length/2; if(Key = =Srcarray[mid]) {               returnmid; }             intStart = 0; intEnd = Srcarray.length-1;  while(Start <=end) {Mid= (End-start)/2 +start; if(Key <Srcarray[mid]) {End= Mid-1; } Else if(Key >Srcarray[mid]) {Start= Mid + 1; } Else {                   returnmid; }           }           return-1; } } `

Binary search Algorithm Java

Related Keywords:
Related Article

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.