Algorithm prerequisites:
==>> Must be sorted by keyword size
The algorithm logic is as follows:
1. Compare the median value in each de-array with the searched Value
2. if the median value is smaller than the value to be searched, select the array on the right of the median value and repeat 1 until an array element equal to the value to be searched is found or a value is returned, indicates that the value to be searched does not exist in the array.
3. if the median value is greater than the value to be searched, select the array on the left of the median value and repeat 1 until an array element equal to the value to be searched is found or a value is returned, indicates that the value to be searched does not exist in the array.
The following is my personal code implementation:
[] A = {1, 3, 4, 5, 6, 8, 9, 13, 14, 17, 21 x = 21 index = String result = index =-1? "The queried value [" + x + "] is not in the array! ":" The queried value ["+ x +"] is in the array and the subscript is: "+ dichotomy ([], (a = NullPointerException ("the array cannot be blank! "} Index = 0 last =. length-1 middle = 0 (index> = 0 & index <= print (a, index, last + 1 middle = (index + last) /2 (a [middle] = (a [middle] <index = middle + 1 (a [middle]> last = middle-1-1 print ([], index, (! = & Index> = 0 & index <= StringBuffer buffer = (I = index; I <last; I ++ buffer. append (a [I] + "" System. out. println ("the array to continue searching is:" +}
Running effect:
The array to continue searching is: 1 3 4 5 6 8 9 13 14 17 219 13 14 17 2117 212121] in the array, and the subscript is: 10
The array to continue searching is: 1 3 4 5 6 8 9 13 14 17 219 13 14 17 2117 2121212] Not in the array!
The array to continue searching is: 1 3 4 5 6 8 9 13 14 17 219 13 14 17 219 131313] in the array, and the subscript is: 7
The array to continue searching is: 1 3 4 5 6 8 9 13 14 17 211 3 4 5 61 31] in the array, and the subscript is: 0
The array to continue searching is: 1 3 4 5 6 8 9 13 14 17 218] in the array, and the subscript is: 5