二分查找
2016-10-20 11:20
253 查看
1.二分查找时需要的是有顺序的
public class BiSeach
{
public static int biseach(int[] arr,int elem)
{
int low=0;
int heigh=arr.length-1;
int mid=(low+high)/2
if(mid==elem)
{
return mid+1
else if(arr[mid]<elem)
{
low =mid + 1;
}
else{
heigh =mid -1
}
return -1
}
}
public static void main(String arg[] s)
{
int[] arr=[12,23,25,54,68,78,79,96]
int elem=54;
int res=biseach(arr,elem)
if(res!=-1)
{
syso("查找的数为"+res)
}
else("不存在")
}
}
public class BiSeach
{
public static int biseach(int[] arr,int elem)
{
int low=0;
int heigh=arr.length-1;
int mid=(low+high)/2
if(mid==elem)
{
return mid+1
else if(arr[mid]<elem)
{
low =mid + 1;
}
else{
heigh =mid -1
}
return -1
}
}
public static void main(String arg[] s)
{
int[] arr=[12,23,25,54,68,78,79,96]
int elem=54;
int res=biseach(arr,elem)
if(res!=-1)
{
syso("查找的数为"+res)
}
else("不存在")
}
}
相关文章推荐