Example of O(n!)?

后端 未结 16 2425
渐次进展
渐次进展 2020-11-30 22:20

What is an example (in code) of a O(n!) function? It should take appropriate number of operations to run in reference to n; that is, I\'m asking a

16条回答
  •  爱一瞬间的悲伤
    2020-11-30 22:48

    I think I'm a bit late, but I find snailsort to be the best example of O(n!) deterministic algorithm. It basically finds the next permutation of an array until it sorts it.

    It looks like this:

    template  
    void snail_sort(Iter first, Iter last)
    {
        while (next_permutation(first, last)) {}
    }
    

提交回复
热议问题