如下
package com.company;
public class Main {
public static void main(String[] args) {
// write your code here
int[] arr=new int[]{1,2,3,4,5,6,7,8,9};
int target=3;
int x=dichotomy(arr,target);
System.out.println(target+"在数组的下标为"+x);
}
public static int dichotomy(int[] arr,int target){
int low=0;
int high=arr.length-1;
for (int i=0;;i++){
int mid=(low+high)/2;
if (arr[mid]<target){
low=mid+1;
}
else if (arr[mid]>target){
high=mid-1;
}
else{
return mid;
}
}
}
}