您的位置:首页 > 其它

二分查找

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("不存在")

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: