算法分析是关于计算机程序性能(performance)与资源利用(communication, memory)的理论研究。Correctness, simplicity, maintainability, cost(time, robustness)feature, security, user-friendliness et al. may important than performance. performance会决定算法的feasible or infeasible,是算法实现的基础。performance应用于尚未成熟领域。
sorting problem:input sequence<a1,a2,……an>, output is permutation(rearrangement) of those numbers<b1,b2,……bn>, where b1<=b2<=……<=bn。
插入排序Insertion sort: sorting A[1..n]
for j <- 2 to n
do key <- A[j]
i <- j-1
while i > 0 and A[i] > key
do A[i+1] <- A[i]
i <- i-1
A[i+1]<-key
running time:input data itself(sorted), input data size(parametrize in input size), upper bounds(guarantee to user)
Worst-case analysis(usually): T(n):max time on any input of size n
Average-case analysis(sometimes): T(n):expected time over all inputs of size n(make an assumption of the statical distribution of the inputs -> expected time, uniform distribution)
Best-case(bogus假象):
for insertion sort:Asymptotic analysis 渐近分析(ignore machine-dependant constants, look the growth of actual running time)
Worst-case: (in reverse order)T(n)=n^2,适合小数据,不适合大数据
并归排序Merge sort: sorting A[1..n]
1. if n = 1, done
2. recursively sort A[1..n/2] and A[n/2+1..n]
3. Merge 2 sorted lists
Key subroutine: Merge, Time = n。
T(n) = 2T(n/2) + n , if n>1
~Recursion tree. -----T(n) = nlog n