Binary search



For interview prep, why else?


April 12th, 2021



int BinarySearch(
    int[] array,
    int value,
    int start,
    int end)
{
    if (start > end)
    {
        return -1;
    }

    int middle = (start + end) / 2;

    if (array[middle] == value)
    {
        return middle;
    }

    if (value < array[middle])
    {
        return BinarySearch(array, value, start, middle - 1);
    }
    else
    {
        return BinarySearch(array, value, middle + 1, end);
    }
}

This site is open source. Improve this page »