complexity of recursive string permutation function

后端 未结 3 1710
耶瑟儿~
耶瑟儿~ 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:35

    Without looking too deeply at your code, I think I can say with reasonable confidence that its complexity is O(n!). This is because any efficient procedure to enumerate all permutations of n distinct elements will have to iterate over each permutation. There are n! permutations, so the algorithm has to be at least O(n!).

    Edit:

    This is actually O(n*n!). Thanks to @templatetypedef for pointing this out.

提交回复
热议问题