美文网首页
二分查找

二分查找

作者: d5cbd4f07363 | 来源:发表于2019-05-21 10:41 被阅读0次

/**

  • a 为数组,low:最小数 ,high:最大数 value:需要查找的值 时间复杂度 a.count,a.count/2,a.count/4,a.count/2^k (k为循环的次数), a.count/2^k >=1所以 k = log2(a.count) (2为底,a.count对数)
    时间复杂度O(log2(a.count))
    /
    int BrinarySearch(int
    a,int low,int high,int value){
    if (low > high) {
    return -1;
    }
    int mid = (low + high)/2;
    if (value == a[mid]) {
    return a[mid];
    }
    if (a[mid] > value) {
    BrinarySearch(a, low, mid -1, value);
    }else {
    BrinarySearch(a, mid+1, high, value);
    }
    return -1;
    }

相关文章

网友评论

      本文标题:二分查找

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