Analysis of Algorithm 2: Master theorem & Math induction

来源:互联网 发布:航测内页成图软件 编辑:程序博客网 时间:2024/05/18 15:07

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)=Θ(nlogablogn)

  • 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

    =.........

    =nlogn+n

    T(n)=nlogn


  • 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 cn (for merge cost: each level will cost cn to merge all sub-problems ). The total cost, therefore, is cnlogn+cn, which is Θ(nlogn).
    这里写图片描述

Ex.2 Insertion Sort

  • Math induction:

     In the Worst-case: T(n)=i=2nini=2i di12n2Θ(n2)

  • We can express insertion sort as a recursive procedure as follows. In order to sort A[1..n], we recursively sort A[1..n1] and then insert A[n] into the sorted array A[1..n1]. Write a recurrence for the running time of this recursive version of insertion sort.

    T(n)={1 , n =1T(n1)+n , n > 1

     for the nth loop, it will cost n1 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(i1)

    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