//二分查找算法的实现 public static int binarySearch(int[] arr,int search) { int low=0; int high=arr.length-1; //找到中间的索引 int middleIndex=0; while (lowsearch) { high=middleIndex-1; } else { low=middleIndex+1; } } return middleIndex; }
1 调用
int[] arr=new int[] {1,2,3,4,5,6,7,8,9,10};
int index= binarySearch(arr, 8); System.out.println("查找的值8位于数组中的索引为:"+index);
默认传入的数组是排好序的