美文网首页
android源码里面的二分法查找

android源码里面的二分法查找

作者: 坠叶飘香 | 来源:发表于2019-05-29 14:23 被阅读0次

    frameworks\base\core\java\android\util\ContainerHelpers.java

    // This is Arrays.binarySearch(), but doesn't do any argument validation.
    static int binarySearch(int[] array, int size, int value) {
      int lo = 0;
      int hi = size - 1;
    
      while (lo <= hi) {
        final int mid = (lo + hi) >>> 1;
        final int midVal = array[mid];
    
        if (midVal < value) {
          lo = mid + 1;
        } else if (midVal > value) {
          hi = mid - 1;
        } else {
          return mid;  // value found
        }
      }
      return ~lo;  // value not present
    }
    

    相关文章

      网友评论

          本文标题:android源码里面的二分法查找

          本文链接:https://www.haomeiwen.com/subject/ancytctx.html