Deep full permutation algorithm and its implementation method _c language

Source: Internet
Author: User
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.
First, recursive implementation
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 collection (A,b,c,d) are composed of the following arrangement:
(1) Following the arrangement of a beginning with a (b,c,d)
(2) The arrangement of following (A,c,d) followed by the beginning of B
(3) The arrangement of following (A,b,d) followed by the beginning of C
(4) The arrangement of the following (A,b,c) followed by the beginning of D, which is clearly a recursive idea, and we have achieved:
Copy Code code as follows:

#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;
}

II. Implementation of STL
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:
Copy Code code as follows:

#include "iostream"
#include "algorithm"
using namespace Std;
void permutation (char* str,int length)
{
Sort (str,str+length);
Todo
{
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;
}

The whole arrangement with certain constraint conditions
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.
Copy Code code as follows:

#include "iostream"
#include "algorithm"
using namespace Std;
void permutation (int* a,int length)
{
int I,flag;
Sort (a,a+length);
Todo
{
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.