标题: 二分法求索引值 [打印本页] 作者: 正哥敲代码 时间: 2016-8-4 10:54 标题: 二分法求索引值 public class demo08 {
public static void main(String[] args) {
int[] arr = {10,15,20,30,35,50};
int index = searchHalf(arr,20);
System.out.println("元素所在的索引值是:" + index);
}
public static int searchHalf(int[] arr,int target){
int max = arr.length-1;
int min = 0;
int mid = (max+min)/2;
while(true){
if(target<arr[mid]){
max = mid - 1;