#演算法#二分搜尋

weixin_33860722發表於2017-05-05
template <class Type>
int LankeHelper::BinarySearch(Type a[], const Type& x, int n)
{
    int left = 0;
    int right = n-1;
    while(left<right)
    {
        int middle= (left + right)/2;
        if(x==a[middle])
        {
            return middle;
        }
        if(x>a[middle])
        {
            left = middle + 1;
        }
        else
        {
            right = middle - 1;
        }
    }
    return -1;
}

相關文章