1 算法特性:
1 输入
2 输出
3可行性
4有穷性
5 确定性
2 时间复杂度
运算规则:
按数量级递增排列,常见的时间复杂度函数有:
常数阶O(1)
对数阶O( logn)
线性阶O(n)
线性对数阶O(nlogn)
平方阶O(n2)
立方阶O(n3)
,…,
k次方阶O(nk),
指数阶O(2n)。
随着问题规模n的不断增大,上述时间复杂度不断增大,算法的执行效率越低。
特殊时间复杂度:
log n 比对数阶时间复杂度小,更好
1 递归算法分析
例:求n!
int fac(int n)
{ if(n==0||n==1) return 1;
else return n*fac(n-1); }
T(n)= 1 n==0或1
T(n)=T(n-1)+1
因此T(n)=T(n-1)+1=T(n-2)+2
=…=T(1)+n-1=n=O(n)
2 折半查找 算法分析
Int BinSe2(int r[], int target, int low, int high)
{
if(low > high)
return false;
else
{ int mid = (low+high)/2;
if(target < r[mid])
return BinSe2(r,target,low,mid-1);
else if(target > r[mid])
return BinSe2(r,target,mid+1,high);
else return true;
}
时间复杂度递归方程求解
一 迭代法/Iteration方法
1. 不断用递推方程的右部替换左部
2.每次替换,随着 n 的降低,在和式中多出一项
3.直到出现初值,停止迭代
4.将初值代入并对和式求和
5. 可用数学归纳法验证解的正确性
例子: 汉诺塔问题
void hanoi(int n,char one,char two,char three) {
if(n==1)
move(one,three);
else
{ hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}
二 Master方法
第三种情况下,一定要再判断一下是否c存在
下面例4 ,就不是多项式的大于