public class binSearch { public static void main(String[] args) { // TODO 自动生成方法存根 int a [] = {0 ,1,2,3,5,7,8,9,23,45,67};// 不算0位置,数组1-n int find = 9; int location = new binSearch().bSearch(a, find, 1, 8); System.out.println("location:"+location); } public int bSearch(int [] a ,int x, int l,int r){ while(l<=r){ int m =(l+r)/2; if(a[m] ==x) return m ; if(a[m] < x){ l = m+1; }else{ r = m-1; } } return -1; } }
二分法搜索
最新推荐文章于 2023-01-07 20:29:55 发布