📜  二进制搜索 c++ 代码示例

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

代码示例1
int binarySearch(int arr[], int l, int r, int x)
{
    if (r >= l) {
        int mid = l + (r - l) / 2;
        if (arr[mid] == x)
            return mid;
        if (arr[mid] > x)
            return binarySearch(arr, l, mid - 1, x);
        return binarySearch(arr, mid + 1, r, x);
    }
    return -1;
}