I. Overview
The whole arrangement is used in many programs, is a very common algorithm, the conventional algorithm is a recursive algorithm, this algorithm is based on the following analysis ideas. Given a set of n elements (n>=1), all possible permutations of the elements in this collection are required to be exported. Second, recursive implementation
2.1. Example One
For example, if the collection is {a,b,c}, so all the permutations of the elements in this set are {(A,b,c), (A,c,b), (B,a,c), (B,c,a), (C,a,b), (C,b,a)}, obviously, given that n elements have a different arrangement of n!, if the given set is {a , b,c,d}, you can use the simple algorithm given below to produce all of its permutations, that is, all permutations of the set (A,b,c,d) are composed of the following permutations: (1) The arrangement of (2) followed by (B,c,d) after the beginning
of a b followed by (
a,c,d) (3) The arrangement of (4) followed by (A,b,d) after the beginning of C followed by (
a,b,c), which is clearly a recursive idea, so we have the following implementations:
#include "iostream"
using namespace std;
void permutation (char* a,int k,int m)
{
int i,j;
if (k = = m)
{for
(i=0;i<=m;i++)
cout<<a[i];
cout<<endl;
}
else
{for
(j=k;j<=m;j++)
{
swap (a[j],a[k]);
Permutation (a,k+1,m);
Swap (a[j],a[k]);
}} int main (void)
{
char a[] = "abc";
cout<<a<< the results of all permutations are: <<endl;
Permutation (a,0,2);
System ("pause");
return 0;
}
2.2. STL implementation
Sometimes the efficiency of recursion makes us have to consider other implementations, and many of the algorithms that convert recursive algorithms to non recursive forms are more difficult, so let's not forget the algorithms that the standard Template Library has implemented, which makes it very easy. The STL has a function next_permutation (), which is useful if, for a sequence, there is the next permutation of this arrangement following the dictionary sort, returns True and produces the permutation, otherwise returns false. Note that in order to produce a full arrangement, this sequence is ordered, that is, to call the sort once. The implementation is simple, let's look at the code:
#include "iostream"
#include "algorithm"
using namespace std;
void permutation (char* str,int length)
{
sort (str,str+length);
Do
{for
(int i=0;i<length;i++)
cout<<str[i];
cout<<endl;
} while (Next_permutation (str,str+length));
}
int main (void)
{
char str[] = "ACB";
cout<<str<< the results of all permutations are: <<endl;
Permutation (str,3);
System ("pause");
return 0;
}
2.3, a certain constraint conditions of the full arrangement
Logarithmic 1,2,3,4,5 to achieve full sorting. Request 4 must be on the left of 3, the other number is at random.
idea: First Use one of the top 2 methods to achieve the full arrangement, and then the full arrangement of the filter, filter out 4 in 3 to the left of the arrangement.
#include "iostream" #include "algorithm" using namespace std;
void permutation (int* a,int length) {int i,flag;
Sort (a,a+length);
Do {for (i=0;i<length;i++) {if (a[i]==3)
flag=1;
else if (a[i]==4)//If 3 is on the left of 4, the flag is 2 flag=2;
if (flag==1)//If 4 is on the left of 3, the flag is 1 {for (i=0;i<length;i++)
cout<<a[i];
cout<<endl;
}}while (Next_permutation (a,a+length));
int main (void) {int i,a[5];
for (i=0;i<5;i++) a[i]=i+1;
printf ("All 4 of the total in the 3 left of%d is: \ n", i);
Permutation (a,5);
System ("pause");
return 0; }
Go from: http://blog.csdn.net/hackbuteer1/article/details/6657435