public class xiawu{
public static void main(String[] args){
int[] array={1,2,3,4,5,6};
int number=binarySearch(array,3);
System.out.println(number);
}
public static int binarySearch(int[] a,int key){
int left=0;
int right=a.length-1;
int mid=0;
while(left<=right)
{
mid=left+right/2;
if(a[mid]>key)
{
right=mid-1;
}
else if(a[mid]<key)
{
left=mid+1;
}
else
{return mid;
}
}
return -1;
}
}
【Java】二分查找
最新推荐文章于 2024-02-22 16:25:59 发布