Full Arrangement Algorithm and implementation

Source: Internet
Author: User

Full arrangement is applied in many programs. It is a non-common algorithm, and a conventional algorithm is a recursive algorithm. This algorithm is obtained based on the following analysis ideas. Given a set with n elements (n> = 1), all possible arrangement of elements in the set must be output.

1. Recursive Implementation

For example, if the set is {a, B, c}, all elements in this set are arranged in {(A, B, C), (A, C, B ), (B, A, C), (B, C, A), (C, A, B), (C, B, A)}, apparently, the given n elements share n! For example, if the given set is {a, B, c, d}, you can use the simple algorithm given below to generate all the arrays, that is, the set (a, B, c, d) All are arranged as follows:

(1) The sequence following (B, c, d) starting with

(2) Arrange (a, c, d) after starting with B

(3) Arrange (a, B, d) after starting with C

(4) Following (a, B, c) after starting with D, this is obviously a recursive idea, so we get the following implementation:

# 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 "; the result of cout <A <"in full order is:" <Endl; permutation (A,); System ("pause"); Return 0 ;}

Ii. STL implementation

Sometimes, the recursive efficiency makes it difficult for us to consider other implementations. It is more difficult to convert recursive algorithms to non-recursive algorithms than recursive algorithms, at this time, we should not forget the algorithms implemented by the standard template library, which makes it very easy. STL has a function next_permutation (), which is used to assume that for a sequence, there is a next arrangement after sorting according to the dictionary, then true is returned and this arrangement is generated, otherwise, false is returned. Note: In order to generate a full arrangement, this sequence is ordered, that is, to call sort once. The implementation is very easy. Let's take a 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 result of full sorting is: "<Endl; permutation (STR, 3); System (" pause "); Return 0 ;}

3. Full arrangement with certain constraints

Perform full sorting on the numbers 1, 2, 3, 4, and 5. Requirement 4 must be on the left of 3, and the other numbers are arbitrary.

Idea: first, use one of the above two methods to implement full sorting, then filter the full sorting, and filter out the four on the left of 3.

# 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, after the code is run, the flag is 2 flag = 2;} If (flag = 1) // assume that 4 is on the left of 3 and the code is run. 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 ("% d all 4 on the left of 3 the result 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.