1. Two points lookup is also called binary lookup, it is a more efficient way to find.
2. Two-point lookup requirements: (1) The sequential storage structure (2) must be used. Must be sorted by keyword size
3. Principle: The array is divided into three parts, followed by the median (the so-called median is the middle of the array of values) before, median, median, the value to be looked up and the median value of the array, if less than the median value in front of the median value is found, if the value is greater than the median value is returned after the median Then there is a recursive procedure, in which the first half or the second part continues to be decomposed into three parts.
4. Implementation: Binary lookup implementation by recursion and loop two ways
5. Code:
1 PackageOther ;2 3 Public classBinarySearch {4 /*5 * Loop Implementation binary Lookup algorithm arr already ordered array x number of lookups required-1 Unable to find data6 */7 Public Static intBinarySearch (int[] arr,intx) {8 intLow = 0; 9 intHigh = Arr.length-1; Ten while(Low <=High ) { One intMiddle = (low + high)/2; A if(x = =Arr[middle]) { - returnMiddle; -}Else if(X <Arr[middle]) { theHigh = Middle-1; -}Else { -Low = middle + 1; - } + } - return-1; + } ARecursive implementation of binary search at Public Static intBinarySearch (int[] DataSet,intDataintBeginindex,intEndIndex) { - intMidindex = (beginindex+endindex)/2; - if(Data <dataset[beginindex]| | data>dataset[endindex]| | Beginindex>EndIndex) { - return-1; - } - if(Data <Dataset[midindex]) { in returnBinarySearch (dataset,data,beginindex,midindex-1); -}Else if(data>Dataset[midindex]) { to returnBinarySearch (dataset,data,midindex+1, EndIndex); +}Else { - returnMidindex; the } * } $ Panax Notoginseng Public Static voidMain (string[] args) { - int[] arr = {6, 12, 33, 87, 90, 97, 108, 561 }; theSystem.out.println ("Loop lookup:" + (BinarySearch (arr, 87) + 1)); +SYSTEM.OUT.PRINTLN ("Recursive lookup" +binarysearch (arr,3,87,arr.length-1)); A } the}
Binary lookup Algorithm (JAVA)