常用查找算法总结

Posted by 大雁小鱼的博客 on May 28, 2018

折半查找

public int bsearch(int[] arr,int left,int right,int v){
     int m = 0;
     while(left < right){
         m =(left+right)/2;
         if(arr[m] == v) {
             return m;
         }
         else if(arr[m] > v) {
             right = m;
         }
         else{
             left = m+ 1;
         }
     }
     return -1;
}