public class FindInHalf {
public static void main(String[] args) {
int target = 8; int[] arrays = {12,3,45,6,1,34,2,5,8}; int locate = halfinfind(target, arrays); if(locate < 0) { System.out.println("数组中不存在值为"+target+"的整数"); }else { System.out.println("数组中存在值为"+target+"的整数,位置在"+locate); } } public static int halfinfind(int target, int[] arrays) { int len = arrays.length; int start = 0; int end = len-1; if(start < end) { int half = 0; while(start<=end) { half = (start + end)/2; if(target == arrays[half]) { return half; }else if(target < arrays[half]) { end = half - 1; }else { start = half + 1; } } }else if (start == end){ if(target == arrays[start]) { return start; }else { return -1; } }else { return -1; } return -1; }}