Skip to content

Latest commit

 

History

History
24 lines (19 loc) · 461 Bytes

binary_search.md

File metadata and controls

24 lines (19 loc) · 461 Bytes

Binary Search Algorithm

Implementations:

int binarySearch(int numbers[], int key, int low, int high) {
    if (low > high) {
        return -1;
    }

    int middle = (high + low)/2;

    if (numbers[middle] == key) {
        return middle;
    }

    if (numbers[middle] > key) {
        return binarySearch(numbers, key, low, middle -1);
    } else {
        return binarySearch(numbers, key, middle + 1, high);
    }
}