/**
* @author xusulong
* input:已排序的序列A(从小到大),数v
* output:v在A中的位置(下标)
*/
public class BinarySearch {
public static int binerySearch(int[] A, int key, int start, int end) {
int index = 0;
int middle = (start + end)/2;
int middleKey = A[middle];
if(key == middleKey) {
index = middle;
} else if(key < middleKey){
index = binerySearch(A, key, start, middle);
} else {
index = binerySearch(A, key, middle + 1, end);
}
return index;
}
public static void main(String[] args) {
int[] A = new int[] {1,2,3,4,5,6,7,8,9,10};
int key = 1;
System.out.println(binerySearch(A, key, 0, 9));
}
}