美文网首页
BinarySearch

BinarySearch

作者: hello3241 | 来源:发表于2017-05-23 21:17 被阅读0次
         public static int rank(int key, int[] arr) {
            int smallTag = 0;
            int bigTag = arr.length - 1;
    
            while (smallTag < bigTag) {
                int mid = smallTag + (bigTag - smallTag) / 2;
                if (key < arr[mid]) {
                    bigTag = mid - 1;
                } else if (key > arr[mid]) {
                    smallTag = mid + 1;
                } else {
                    return mid;
                }
            }
            return -1;
        }
    

    相关文章

      网友评论

          本文标题:BinarySearch

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