java实现二分查找

 1 /**
 2      * 二分查找
 3      * @param a
 4      * @param n
 5      * @param value
 6      * @return
 7      * @date 2016-10-8
 8      * @author shaobn
 9      */
10         public static int binaryFind(int[] a,int n,int value){
11             int lowNum = 0;
12             int highNum = n-1;
13             while(lowNum<=highNum){
14                 int midNum = (lowNum+highNum)/2;
15                 if(a[midNum]==value){
16                     return midNum;
17                 }else if (a[midNum]>value) {
18                     highNum = midNum-1;
19                 }else if (a[midNum]<value) {
20                     lowNum = midNum+1;
21                 }
22             }
23             return -1;            
24         }

 

posted @ 2016-10-08 08:59  邻家小书童  阅读(275)  评论(0编辑  收藏  举报