二分查找


public class BinarySearch {
	
//目标值为arrow
	int binarySearch(int a[],int arrow){
		
		
		int low=0;int high=a.length-1;
		int mid;
		while(low<=high)
		{   
			mid=(low+high)/2;
			if(a[mid]>arrow){
				//左边
				high=mid-1;
			}else if(a[mid]<arrow){
				//右边
				low=mid+1;
			}else {
				return mid;
			}
		}
		return -1 ;
	}
}

你可能感兴趣的:(二分查找)