二分查找
来源:互联网 发布:废铁战士的时光机 知乎 编辑:程序博客网 时间:2024/06/14 18:36
package paixu;
public class erfenchazhao {
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer a[]={1,2,3,4,6,8,9,10};
System.out.println(binarySearch(a,3));
}
public static int binarySearch(Integer[] a, int ycds) {
int low=0;
int high=a.length-1;
while(low<=high)
{
int mid=(low+high)/2;
if(ycds==a[mid])
return mid;
else if(ycds<a[mid])
{
high=mid-1;
}else if(ycds>a[mid])
{
low=mid+1 ;
}
}
return -1;
}
}
public class erfenchazhao {
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer a[]={1,2,3,4,6,8,9,10};
System.out.println(binarySearch(a,3));
}
public static int binarySearch(Integer[] a, int ycds) {
int low=0;
int high=a.length-1;
while(low<=high)
{
int mid=(low+high)/2;
if(ycds==a[mid])
return mid;
else if(ycds<a[mid])
{
high=mid-1;
}else if(ycds>a[mid])
{
low=mid+1 ;
}
}
return -1;
}
}
阅读全文
0 0