顺序查找、二分查找

一、顺序查找
适用范围:没有进行排序的数据序列
缺点:速度非常慢, 效率为O(N)

template <typename Type>  
Type *sequenceSearch(Type *begin, Type *end, const Type &searchValue)  
throw(std::range_error)  
{  
    if ((begin == end) || (begin == NULL) || (end == NULL))  
        throw std::range_error("pointer unavailable");  

    for (Type *index = begin; index < end; ++index)  
    {  
        if (*index == searchValue)  
            return index;  
    }  

    return end;  
}  

template <typename Type>  
Type *sequenceSearch(Type *array, int length, const Type &searchValue)  
throw(std::range_error)  
{  
    return sequenceSearch(array, array+length, searchValue);  
}  

二、迭代二分查找
应用范围:数据必须首先排序,才能应用二分查找;效率为(logN)
算法思想:譬如数组{1, 2, 3, 4, 5, 6, 7, 8, 9},查找元素6,用二分查找的算法执行的话,其顺序为:1.第一步查找中间元素,即5,由于5<6,则6必然在5之后的数组元素中,那么就在{6, 7, 8, 9}中查找。 2.寻找{6, 7, 8, 9}的中位数,为7,7>6,则6应该在7左边的数组元素中,那么只剩下6,即找到了。
二分查找算法就是不断将数组进行对半分割,每次拿中间元素和目标元素进行比较。

template <typename Type>  
Type *binarySearch(Type *begin, Type *end, const Type &searchValue)  
throw(std::range_error)  
{  
    if ((begin == end) || (begin == NULL) || (end == NULL))  
        throw std::range_error("pointer unavailable");  

    /**注意:此处high为end-1,并不是end 
        因为在后续的查找过程中,可能会如下操作 (*high), 或等价的操作 
        此时应该访问的是最后一个元素, 必须注意不能对数组进行越界访问! 
    */  
    Type *low = begin, *high = end-1;  
    while (low <= high)  
    {  
        //计算中间元素  
        Type *mid = low + (high-low)/2;  
        //如果中间元素的值==要找的数值, 则直接返回  
        if (*mid == searchValue)  
            return mid;  
        //如果要找的数比中间元素大, 则在数组的后半部分查找  
        else if (searchValue > *mid)  
            low = mid + 1;  
        //如果要找的数比中间元素小, 则在数组的前半部分查找  
        else  
            high = mid - 1;  
    }  

    return end;  
}  

template <typename Type>  
Type *binarySearch(Type *array, int length, const Type &searchValue)  
throw(std::range_error)  
{  
    return binarySearch(array, array+length, searchValue);  
}  

三、递归

//递归求解斐波那契数列  
unsigned long ficonacciRecursion(int n)  
{  
    if (n == 1 || n == 2)  
        return 1;  
    else  
        return ficonacciRecursion(n-1) + ficonacciRecursion(n-2);  
}  
//非递归求解斐波那契数列  
unsigned long ficonacciLoop(int n)  
{  
    if (n == 1 || n == 2)  
        return 1;  

    unsigned long  first = 1, second = 1;  
    unsigned long  ans = first + second;  
    for (int i = 3; i <= n; ++i)  
    {  
        ans = first + second;  
        first = second;  
        second = ans;  
    }  

    return ans;  
}  

四、递归二分查找
算法思想如同迭代二分查找

template <typename Type>  
Type *binarySearchByRecursion(Type *front, Type *last, const Type &searchValue)  
throw(std::range_error)  
{  
    if ((front == NULL) || (last == NULL))  
        throw std::range_error("pointer unavailable");  

    if (front <= last)  
    {  
        Type *mid = front + (last-front)/2;  
        if (*mid == searchValue)  
            return mid;  
        else if (searchValue > *mid)  
            return binarySearchByRecursion(mid+1, last, searchValue);  
        else  
            return binarySearchByRecursion(front, mid-1, searchValue);  
    }  

    return NULL;  
}  

template <typename Type>  
int binarySearchByRecursion(Type *array, int left, int right, const Type &searchValue)  
throw (std::range_error)  
{  
    if (array == NULL)  
        throw std::range_error("pointer unavailable");  

    if (left <= right)  
    {  
        int mid = left + (right-left)/2;  
        if (array[mid] == searchValue)  
            return mid;  
        else if (searchValue < array[mid])  
            return binarySearchByRecursion(array, left, mid-1, searchValue);  
        else  
            return binarySearchByRecursion(array, mid+1, right, searchValue);  
    }  

    return -1;  
}  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值