您的位置:首页 > 编程语言 > Go语言

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>1

according 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 runtime