源码
public class Dichotomy {
public static void main(String[] args){
int[] array = new int[12];
for(int i=0;i<12;i++){
array[i] = i;
}
System.out.print(dichotomy(array,8));
}
private static int dichotomy(int[] array,int key){
int left = 0,right = array.length-1;
int t;
while (left<=right){
t = (left + right)/2;
System.out.println("left"+left);
System.out.println("right"+right);
System.out.println(t);
if(array[t] == key){
return t;
}
if(array[t] < key){
left = t+1;
}
if(array[t] > key){
right = t-1;
}
}
return -1;
}
}