complexity of recursive string permutation function

后端 未结 3 1729
耶瑟儿~
耶瑟儿~ 2020-12-31 01:16

From: Are there any better methods to do permutation of string?

what is the complexity of this function???

void permute(string elems, int mid, int en         


        
3条回答
  •  庸人自扰
    2020-12-31 01:42

    Ignoring the print, the recurrence relation satisfied is

    T(n) = n*T(n-1) + O(n)

    If G(n) = T(n)/n! we get

    G(n) = G(n-1) + O(1/(n-1)!)

    which gives G(n) = Theta(1).

    Thus T(n) = Theta(n!).

    Assuming that the print happens exactly n! times, we get the time complexity as

    Theta(n * n!)

提交回复
热议问题