1 回答
botao555
TA贡献48条经验 获得超46个赞
你说的是二分法查找数组中某个元素的下标吧
public class Binary { public static void main(String[] args) { int[] arr = {1,2,3,5,8}; System.out.println(binaryFind(arr,8)); } public static int binaryFind(int[] arr,int value){ int low=0,high=arr.length - 1; while(low <= high){ int midle = (low + high) / 2; midle = (low + high) / 2; if(arr[midle] == value){ return midle; } if(arr[midle] > value){ high = midle + 1; }else{ low = midle + 1; } } return -1; } }
添加回答
举报
0/150
提交
取消