Find java dichotomy principle

Find principle dichotomy:

      1, as long as the low index not greater than the high index, it is binary search (step 1-3)

      2, first in an ordered array in half to find the middle of the coordinates, and if successful you are looking subscript equal, find the target number, then half ended.

      3, if the step 2 is not found, it will appear to two cases: a, find value greater than successful; B, find value less than successful;

        3.1, if the bid is greater than the value of the find, bid value indicates that find the left, so that the high bid at that time, and then continue bipartite

        3.2, if the bid is less than the find value, indicating successful find the right value, then the bid is low at this time, then continue bipartite

      4, if the low index greater than high subscript: that is not the find the value you want to find, and low and high must be the next phase (similar: low [4], high [3], return -5). This return value should find the inserted negative superscript -1 namely: -low - 1

 

 

Find dichotomy disadvantages:

      1, the array must be ordered array.

   

Find the dichotomy advantages:

      1. Find a number of small, high efficiency.

 

 

Case:

import java.util.Arrays;

public  class TestBinarySearch {
     public  static  void main (String [] args) {
         int [] = {5,13,19,21,37,56,64,75,80,88,92 ARR};     // array must have sequence
        
        int Low = 0;     // low 
        int High = arr.length;     // high 
        int MID = 0;     // can be easily assigned to the bit index
        
        int Find = 37 [;     // Find the value of
        
        boolean flag = false;
        
        the while (Low <= High) {         // long upper side than low small, continue bipartite 
            ; MID = (Low + High) / 2     // bit than find value; the median is larger than find values: There are three cases two minutes after small; is equal to the median value and find
            
            IF (ARR [MID]> find) {     // median ratio find large values after two minutes, find the value described in the right position, then the upper bit is the case, then continue bipartite 
                High MID = -. 1 ;
                
            } The else  IF (ARR [MID] <find) {     // median smaller than find a value of two points, indicates that find value in the left position, the lower is the position at this time, then continue binary 
                Low = MID +. 1 ;
            } The else {                         // equal among the bit values and find, to find find the corresponding index values, two end points 
                In Flag = to true ;
                System.out.println(mid);
                break;
            }
            
        }
        
        if(flag){                
                System.out.println("mid=" + mid +"========"+ "find="+ arr[mid]);                
            }else{
                System.out.println ( -low - 1);     // if not found, it is certainly greater than the low high, low and high and the next phase, the return value of the subscript position find no where to be found (ie -low --1) 
            }
    }
}

 

Guess you like

Origin www.cnblogs.com/eyjdbk/p/11028120.html