算法学习之Honer's Rule

    The usual way to evaluate such polynomials is to use Horner's rule, which is an algorithm to compute the summation without requiring the computation of arbitrary powers of x.

    Horner's Rule用于多项式求和,它的主要优势就是降级运算,即将求幂运算变为乘法和加法运算。链接:Horner's Rule

int Horner(int a[], unsigned int n, int x)
{
    int result = a[n];
    for (int i = n - 1; i >= 0; --i)
        result = result * x + a[i];
    return result;
}


    算法分析:有效运算代码从第3行到第6行

row 3  : 3 * T(fetch) + T[.] + T(store)

注:数组元素的取值操作事实上经过了4个步骤,一获取数组首地址,二获取下标值,三计算待获取元素的地址T[.],四取元素值。

row 4a : 2 * T(fetch) + T(-) + T(store) 

row 4b : ( 2 * T(fetch) + T(<)) * (n + 1)

row 4c : ( 2 * T(fetch) + T(-) + T(store)) * n

row 5  : ( 5 * T(fetch) + T[.] + T(*) + T(+)+ T(store)) * n

row 6  : T(fetch) + T(return)

注:函数调用的时间放在调用者端计算,函数返回的时间放在被调函数的整个开销中


从上面的分析中可知:

1,该算法的时间复杂度是 T(n),线性关系。

2,该算法的核心思想是累积,即将前一次的迭代的结果直接放入下一次迭代,参与运算。


你可能感兴趣的:(算法)