📜  二分查找返回索引c++代码示例

📅  最后修改于: 2022-03-11 14:44:45.123000             🧑  作者: Mango

代码示例1
int binarySearch(vector& arr, int p, int r, int num) {
        while (p <= r) {
            int mid = (p + r)/2;
            
            if (arr[mid] == num) return mid;
            else if (arr[mid] > num) r = mid-1;
            else  p = mid+1;
        }
        return -1;
    }