one or two-point lookup method
The binary search is to compare the key of the lookup with the middle key of the sub-array, if the key found is less than the middle key, the left sub-array continues to find, if it is greater than the middle key, it is found in the right sub-array, otherwise the middle key is the element to be searched.
@Test Public voidsearchduty () {integer[]t=Newinteger[]{1,2,3,4,5,6};//Original ArrayInteger key=10;//Target ElementInteger Start= 0;//Start PositionInteger end=t.length-1;//End Position while(start<=end) {Integer Index= (start+end)/2; if(key==T[index]) {System.out.println ("Coordinates [" +index+ "]"); return; }Else if(Key <T[index]) {End=index-1; }Else{Start=index+1; }} System.out.println ("Not Found"); }
Note: The precondition for binary lookup (binary lookup) is that ordered table sequential storage is required, and for static lookup tables, once the sorting is no longer changed, binary lookup can get good efficiency. However, for datasets that require frequent insert or delete operations, maintaining an ordered ordering can create a lot of work, and it is not recommended to use
Java data structure and algorithm search method