Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

recursive binary search

int binarySearch(int[] A, int low, int high, int x)
{
    if (low > high) {
        return -1;
    }
    int mid = (low + high) / 2;
    if (x == A[mid]) {
        return mid;
    }
    else if (x < A[mid]) {
        return binarySearch(A, low,  mid - 1, x);
    }
    else {
        return binarySearch(A, mid + 1,  high, x);
    }
}
Source by iq.opengenus.org #
 
PREVIOUS NEXT
Tagged: #recursive #binary #search
ADD COMMENT
Topic
Name
9+9 =