BinarySearch

     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)