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.
Examples of binary lookup algorithms
For a given sequence (ordered) {3,5,11,17,21,23,28,30,32,50,64,78,81,95,101}, press the binary lookup algorithm to find the data element with the keyword value 81.
Algorithm discussion for binary lookup:
Pros: asl≤log2n, that is, every time a comparison is done, the look-up range is reduced by half. The search process can be completed by log2n.
Cons: Because of ordered order, it is necessary to order the search sequence, and to sort all data elements by size is a very time-consuming operation. In addition, sequential storage structure of the insertion, deletion operation is inconvenient.
Consider: the ability to discard more parts through a comparison (that is, after a comparison, so that the scope of the search is smaller), in order to achieve the purpose of improving efficiency. ......?
Consider combining the two methods (sequential lookup and binary lookup), that is, order lookup simple and binary find efficient, to achieve the purpose of improving efficiency? In fact, this is the algorithm idea of block lookup.
01. PackageSrc.com.sunchis.basic;02. 03. Public classBinarySearch {04./**05. * Binary Search algorithm 06. * 07. * @paramSrcarray ordered array 08. * @paramkey to find element 09. * @returnThe array subscript of key, not found returns-1 10. */11. Public Static voidMain (string[] args) {12.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)); 14. } 15. 16.//recursive implementation of binary search17. Public Static intBinsearch (intSrcarray[],intStartintEndintkey) { 18.intMid = (End-start)/2 +start; 19.if(Srcarray[mid] = =key) { 20.returnmid; 21st. } 22.if(Start >=end) { 23.return-1; 24.}Else if(Key >Srcarray[mid]) { 25.returnBinsearch (Srcarray, Mid + 1, end, key); 26.}Else if(Key <Srcarray[mid]) { 27.returnBinsearch (Srcarray, start, mid-1, key); 28. } 29.return-1; 30. } 31. 32.//Two-point lookup for Common Loop implementations33. Public Static intBinsearch (intSrcarray[],intkey) { 34.intMID = Srcarray.length/2; 35.if(Key = =Srcarray[mid]) { 36.returnmid; 37. } 38. 39.intStart = 0; 40.intEnd = Srcarray.length-1; 41. while(Start <=end) { "Mid = (End-start)/2 +start; 43.if(Key <Srcarray[mid]) { The. End = Mid-1; 45.}Else if(Key >Srcarray[mid]) { Start = mid + 1; 47.}Else { 48.returnmid; 49. } 50. } 51.return-1; 52. } 53.}
Java, binary search method, online lookup