CSDN博客

img shaidanR

[算法]有序二分查找

发表于2004/10/21 16:57:00  677人阅读

分类: C++

const int not Found = -1;

int binSearch ( const vector<int> &vec ,int val )
{
    int low = 0;
    int high = vec.size () -1 ;
    while ( low <= high )
    {
        int mid ( low + high ) / 2 ;
        if ( val == vec [ mid ] )
            return mid ;
        if ( val < vec [mid] )
            high = mid - 1;
        else
            low = mid + 1;
    }
    return notFound ;
}
阅读全文
0 0

相关文章推荐

img
取 消
img