1. 程式人生 > >c 二分查詢

c 二分查詢

int binarySearch(int *a, int n, int data)
{
    int low, mid, high;
    //search fail
    if(a = null) return -1;
    low = 0;
    high = n -1;
    while(lown <= high)
    {
        mid = (low + high) / 2;

        //search success
        if(data == a[mid]) 
        return mid;
        else if(data < array
[mid]) { high = mid -1; } else { low = mid + 1; } } //search fail return -1; }