/**
* TODO
* 二分查找例子
* @author Roy
* @date 2021/2/10 23:10
*/
public class BinarySearch {
public static void main(String[] args) {
int a[] = new int[]{0,1,2,6,7,12,15,78};
int result = binarySearch(a,7);
System.out.println(result);
}
public static int binarySearch(int search[],int target){
int left =0;
int right=search.length-1;
int middle = 0;
int result = 0;
while (left<right){
middle = (left+right)/2;
if(target==search[middle]) return middle;
else if(target<search[middle]) right = middle;
else left = middle+1;
}
return -1;
}
}