Binary search

Hide text Hide pseudo-code

Find the given key from the table by using binary search.


Some additional problems.

int binarySearch(int table[],int x) {
    int low = 0;
    int high = table.length - 1;
    int mid;

    while( low <= high ) 
    {
        mid = (low + high) / 2;

        if( table[mid] < x) low = mid + 1;
        else if(table[mid] > x) high = mid - 1;
        else return mid;
    }
    return -1;     // Not found
}


  Created Fri Oct 30 13:52:51 EET 2009 - Powered by SVG-hut