6
我想知道next_permutation函数的时间复杂度。我可以查看它的代码吗?C++中std :: next_permutation()函数的时间复杂度是多少?
我想知道next_permutation函数的时间复杂度。我可以查看它的代码吗?C++中std :: next_permutation()函数的时间复杂度是多少?
见http://www.sgi.com/tech/stl/next_permutation.html:
线性。最多(最后 - 第一)/ 2 掉期。
要查看源代码,只需查看系统的STL头文件即可。在类似Unix的系统上,您可能需要寻找类似/usr/include/c++/4.1.2/bits/stl_algo.h
的地方。
实际上,它比线性好。这是摊销O(1) - 即,如果你叫它n!次,它只需要O(n!)操作,而不是n * n !.见[这个问题](http://stackoverflow.com/questions/4973077/the-amortized-complexity-of-stdnext-permutation)。 – ShreevatsaR 2011-06-30 11:58:30