// binary search (dichotomy)
public void arrayHalfQuery () {
// definition of an array of type int integer data type
int [] num = {1,3,4,7,9,10,12,15,17 , 19,21,22,25,26,28,29,30};
// define a key value to find
int = 18 is key;
// remove array subscripts maximum and minimum index
int min = 0 ;
int = num.length-max. 1;
// call the infinite loop
the while (to true) {
// definition of half a standard intermediate value lookup
int mID = (max + min) / 2;
// if the key value intermediate value is equal to the target value, then the lookup is successful, and out of the loop
iF (Key == NUM [mID]) {
System.out.println ( "you want to query the index values:" + mID);
BREAK;
// if is greater than the value for key num [mid], left excluded, min moved to the middle of mid + 1
} the else IF (key> NUM [mID]) {
min + 1'd = mID;
When the value is less than the key // num [mid], the right is excluded, max moved to the middle of mid-1
} the else IF (key <NUM [MID]) {
max = MID -1;
}
// if less than max min, the find a complete array explanation has also not found, the end of the infinite loop
// because only three cases key, so if you want to use branching statements alone can not be used if the else
if (max <min) {
System.out.println ( "you to find the value does not exist in the array ");!
BREAK;
}
}
}
An array of binary search (dichotomy) method
Guess you like
Origin www.cnblogs.com/private-mingmie/p/11826660.html
Recommended
Ranking