对于已排序的数组A进行二分查找,实现如下:

int binarySearch(int key){
    int left = 0;
    int right = n;
    int mid;
    while(left < right){
        mid = (left+right)/2;
        if(key == A[mid]) return 1;
        if(key > A[mid]) left = mid+1;
        else if(key < A[mid]) right = mid;
    }

    return 0;
}