Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the time complexity of std::next_permutation() function in c++?

Tags:

c++

stl

I wanted to know the time complexity of the next_permutation function. Can I view its code too ?

like image 767
Vaibhav Avatar asked Feb 11 '11 18:02

Vaibhav


People also ask

What is the function of Next_permutation?

C++ Algorithm next_permutation() function is used to reorder the elements in the range [first, last) into the next lexicographically greater permutation. A permutation is specified as each of several possible ways in which a set or number of things can be ordered or arranged.

What is the time complexity of find in C++?

find(): Searches the string and returns the first occurrence of the parameter in the string. Its time complexity is O(N) where N is the size of the string.

What is lexicographically next greater permutation of numbers?

Lexicographically next permutation in C++ The lexicographically next permutation is basically the greater permutation. For example, the next of “ACB” will be “BAC”. In some cases, the lexicographically next permutation is not present, like “BBB” or “DCBA” etc.


1 Answers

See http://www.sgi.com/tech/stl/next_permutation.html:

Linear. At most (last - first) / 2 swaps.

To see the source code, just look in STL header files for your system. On a Unix-like system, you probably need to look somewhere like /usr/include/c++/4.1.2/bits/stl_algo.h.

like image 169
Oliver Charlesworth Avatar answered Sep 20 '22 10:09

Oliver Charlesworth