二分法查找

二分法查找
class BinarySearch{
public static void main(String[]args){
int[] arr={0,5,7,9,11,15,48,95,102};
int index = ax7(arr,102);
System.out.println(index);
}

public static int ax7(int[] arr,int key){
	int begin = 0;
	int end = arr.length-1;
	while(begin<=end){
		int mid = (begin+end)/2;
		if (arr[mid] == key){
			return mid;
		}else if(arr[mid] < key){
			begin=mid + 1;
		}else end = mid - 1;
	}
	return -1;
}

}

©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页