Binary search also known as binary lookup, the advantage is less than the number of comparisons, Find Fast, the disadvantage is that the unknown origin table is ordered table, and insert delete difficult. Therefore, the binary lookup method is suitable for an ordered list that does not change frequently and finds frequent.
The algorithm requires:
1, the sequential storage structure must be used.
2, must be ordered by the size of the key words.
The time complexity of the algorithm is the worst: O (Logn)
Note that there are mid, low, high
Its Java implementation code is as follows (the code is flawed, just the basic implementation, needs to be perfected):
Public class BinarySearch {
/**
* @param args
*/
Public static void main (string[] args) {
// TODO Auto-generatedmethod stub
int [] src = {1,3,5,7,8,9};
System. out. println (binarysearch(src,3));
}
private static int binarysearch (int[] src,int i) {
int low=0;
int high=src. length-1;
System. out. println ("+low");
System. out. println ("+high");
while (Low<=high) {
int mid = (Low+high)/2;
System. out. println ("Mid is"+mid);
if (src[mid]==i) {
return mid;
}Else if(I<src[mid]) {
High=mid-1;
}Else{
low=mid+1;
}
}
return -1;
}
}
Java implementation binary search \ Binary Lookup