二分查找

来源:互联网 发布:网络大电影没办法发行 编辑:程序博客网 时间:2024/04/30 03:58

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

}

}


0 0
原创粉丝点击