Analysis of Algorithm 2: Master theorem & Math induction
2017-07-08 11:56
573 查看
Using Master theorem to get Running time of Algorithm T(n)
For Divided-and-Conquer to get Running time T(n)Here is the Master theorem
Ex.1 Merge sort
T(n)={Θ(1),n=12T(n/2)+Θ(n),n>1according to Master theorem:∴T(n)=Θ(nlogab⋅logn)
according to Math induction:
T(n)={1,n=12T(n/2)+n,n>1
=2(2T(n/4)+n/2)+n
=2(2T(2T(n/8)+n/4)+n/2)+n
=.........
=n⋅logn+n
∴T(n)=n⋅logn
The fullyexpanded tree in part (d) has logn + 1 levels (i.e., it has height lg n, as indicated), and each level contributes a total cost of c⋅n (for merge cost: each level will cost c⋅n to merge all sub-problems ). The total cost, therefore, is cn⋅logn+cn, which is Θ(nlogn).
Ex.2 Insertion Sort
Math induction:In the Worst-case: T(n)=∑i=2ni≈∫ni=2i di≈12n2≈Θ(n2)
We can express insertion sort as a recursive procedure as follows. In order to sort A[1..n], we recursively sort A[1..n−1] and then insert A[n] into the sorted array A[1..n−1]. Write a recurrence for the running time of this recursive version of insertion sort.
T(n)={1 , n =1T(n−1)+n , n > 1
for the nth loop, it will cost n−1 moves and 1 time insertion in the worst-case
∴T(n)≈12n2≈Θ(n2)
So, the O notation of Insertion Sort is: O(n2)
Ex.3 Bubblesort
math method: T(n)=∑i=1n(i−1)
Explanation: this equation represent that when there are i elements need to be sorted, it will take
exchange i-1 times to place one element in worst-case
∴T(n)≈Θ(n2)
So, the O notation of Bubblesort is: O(n2)
Refs.
http://blog.csdn.net/lanchunhui/article/details/52451362相关文章推荐
- Algorithm Analysis of Traffic Interchange and Transportation system
- [Transshipment] The Analysis of Algorithm of PDF Password Remover v3.0
- Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency
- A brief analysis of vatic & labelme
- algorithm@ Sieve of Eratosthenes (素数筛选算法) & Related Problem (Return two prime numbers )
- how to compile source code of "Data Structures & Algorithm Analysis in Java" writen by Mark Allen Weiss
- What is the Big O analysis of this algorithm?(Stackoverflow)
- Week 1 Analysis of Algorithm(算法分析)
- Data Structures and algorithm analysis—1.2.4&1.2.5Modular Arithmeti&The P Word(数据结构—模数运算&P字)
- Analysis and implement of Consistent Hashing Algorithm
- Illustrating the generic search algorithm: Search for first occurrence of deque's contents as a subs
- Your branch is ahead of 'origin/master' by 1 commit
- MIT算法导论——第一讲.Analysis of algorithm
- Implementation codes of Data Structures and Algorithm Analysis in C (1)
- 第五章 5.1 网页等级算法全面阐释 Analysis of PageRank Algorithm
- Modbus Master & Slave of WinCE on X86
- [note]Mathematical Analysis of Algorithm(DE Knuth)
- 势力图技术:模型表现、算法和参数(The Mechanics of Influence Mapping: Representation, Algorithm & Parameters)
- Summary of Analysis of Algorithm
- A novel Bayesian network inference algorithm for integrative analysis of deep sequencing data