二分查找算法

来源:互联网 发布:双十一淘宝的交易额 编辑:程序博客网 时间:2024/05/22 17:34

    int[] source={-34,4,5,56,75,345,353,555,666};
   int searchKey=555;
        int low=0,high=source.length-1;
       int mid;
while(low<high){

             mid=(low+high)/2;

               if (source[mid]<searchKey) {

                        low=mid+1;
 
                                                       }else {

                               high=mid;
                                                               }
                                                    }
 if (source[low]==searchKey) {
 System.out.println(low+"  "+source[low]);
}else {
system.out.println(" no found");

}

 

0 0
原创粉丝点击