The algorithm for generating full sorting is to enumerate all possible full sorting without repetition and omission for a given character set. The arrangement of any n character sets can be 1 ~ The N numbers of N correspond one to one, so here we take the arrangement of N numbers as an example to describe the generation method of the arrangement.
There is a definite linear order between all the n characters. In all the permutation, except the last one, there is a successor; in addition to the first arrangement, there is a precursor. The successor of each arrangement can be obtained from its precursor with minimal changes. The algorithm for generating a full arrangement is to generate all the Arrangement Methods one by one from the first arrangement.
The following methods are usually used to generate a full array:
Lexicographic Method
Incremental INCREMENT METHOD
Progressive decimal number of digits
Neighbor exchange method
Recursive Algorithms
1. Lexicographic Method
In the lexicographical order method, the order of numbers 1, 2, 3... n is determined by comparing the numbers one by one from left to right. For example, for five numbers, 12354 and 12345 are arranged first and 12345 are arranged later. According to this rule, the top of all the five numbers is 12345, and the last is 54321.
The lexicographic algorithm is as follows:
Set P to 1 ~ A full arrangement of N: P = P1P2 ...... pn = P1P2 ...... pj-1pjpj + 1 ...... pk-1pkpk + 1 ...... pn
1) Start from the right end of the arrangement and find the number J of the first number smaller than the right digit (J starts from the left end ), that is, j = max {I | pI <PI + 1}
2) Find the smallest number in the number on the Right of PJ, k = max {I | pi> PJ} (the number on the right increases progressively from right to left, so K is the number of all numbers greater than PJ)
3) Change Pi, PK
4) then PJ + 1 ...... pk-1pkpk + 1pn reversed to get the arrangement P ''= P1P2 ..... pj-1pjpn ..... PK + 1pkpk-1 ..... PJ + 1, which is the next arrangement of P.
For example, 839647521 is a number from 1 ~ 9. The steps for generating the next arrangement from it are as follows:
From the right to the left, find the first number that is 4 839647521 smaller than the number on the right.
Find the minimum value of the number after this number, which is 5 839647521
Swap 5 and 4 839657421
Reversing 7421 to 839651247
Therefore, the next order of 839647521 is 839651247.
2. increment the number of digits
In the incremental carry-in method, the number of medians is used to find another sort from one sort. If we use ki to arrange P1P2... pi... if the number of elements on the right side of the PI in PN is smaller than that on the Pi, the number of mediation in the arrangement is the corresponding arrangement of K1 ...... ki ...... kn-1.
For example, the number of medians in 839647521 is, and ,...... numbers 8, 3, 9, and ,...... the number of digits smaller than the value on the right.
The number of medians is an intermediate part of the computing arrangement. A known arrangement requires the next arrangement. First, determine the number of medians. The number of medians In the first arrangement is followed by the number of medians in the original arrangement plus 1. Note that, if the last kn-1 of the intermediary number + 1 = 2, it is to carry forward, in general, if Ki + 1 = n-I + 1, it is to carry, this is the so-called incremental carry system. For example, if the number of medians in 839647521 is 72642321, the number of medians in the next arrangement is 67342221 + 1 = 67342300 (because 1 + 1 = 2, so carry forward, 2 + 1 = 3, carry occurs again, so the next median number is 67342300 ).
After obtaining the number of medians, you can sort them according to their restoration.
The algorithm is as follows:
Number of intermediaries K1, K2 ,......, kn-1 of the numerical order of the numbers in the N, n-1 ,......, 2 in the arrangement from the right side of the empty digits, therefore, to K1, K2 ,......, kn-1 value from the right to the left to determine N, n-1 ,...... and the position 2 are placed one by one in the Arrangement: I is placed in the right-start Ki + 1. If a number already exists, this position is not included, and the last vacant space is placed in 1.
Therefore, 67342300 can be obtained from 849617523, which is the last one of 839647521. Because 9 is placed first, k1 = 6, 9 is placed on the right from 7th places, and 6 vacancies are vacant. Then, 8, K2 = 7, 8 are put on the right from 8th places, but 9 occupies one space, therefore, 8 should be placed in the first 9th places on the right, and so on.
3. Decimal carry-in method
In the incremental carry-in method, the bitwise of the intermediary number is every 2 to 1, with frequent carry-in, which is a disadvantage. Flip the incremental carry system to get the descending carry system.
839647521 of the medians are 67342221 (k1k2... kn-1), turning back to 12224376 (kn-1... k2k1), which is the intermediary Number of the descending carry number: ki (I = n-1, N-2 ,..., 2) Every I TO THE ki-1 into 1. Given the arrangement P, the number of medians in the next arrangement of P is defined as the number of medians in P plus 1. For example, P = 839647521, P has 12224376 medians, and P has 12224376 + 1 = 12224377 medians in the next arrangement. Thus, the next arrangement of P is 893647521.
Given the number of medians, you can use a method similar to the incremental carry-in method to restore the order. However, in the decimal carry-in number, you can directly find the next order from an arrangement without first calculating the number of medians. The specific algorithm is as follows:
1) if P (I) = N and I <> N, P (I) is exchanged with P (I-1)
2) If P (n) = N, find a continuous decreasing sequence 9, 8 ,......, I, remove it from the left side of the arrangement, and then add in reverse order to the right side of the arrangement, and then the I-1 and the left side of the number exchange
For example, the next permutation of P = 893647521 is 983647521. In the next arrangement of 983647521, because 9 is on the leftmost side and 2nd is 8 and 3rd is not 7, 8 and 9 are arranged from small to large at the rightmost side 364752189, then, the next order of 7 and its left digit is 983647521. For example, for the next order of 987635421, you only need to sort 9876 from small to large to the rightmost end, and check 5 and 3 on the left side to get 534216789.
4. Adjacent Position swap Method
The next arrangement in the neighbor swap method is always the result of an adjacent pair in the last arrangement. Taking the arrangement of four elements as an example, the last element 4 is exchanged with the previous element one by one, and four new orders can be generated:
1 2 3 4 1 2 4 3 1 4 2 2 3 3
Then, the two elements at the end of the last arrangement are exchanged, and the four elements at the end of the last arrangement are successively exchanged, and four new orders are generated:
4 1 3 2 1 4 3 2 3 3 4 4 2 3 4
Then, the two elements at the end of the last arrangement are exchanged to move 4 forward from the back:
3 1 2 4 3 1 4 2 3 4 1 2 2
In this loop, you can find all the arrays.
5. Value-added element method (n method)
1) from the original arrangement P = P1P2 ...... starting from Pn, the nth bit is added with n-1. If the value of this bit exceeds N, divide it by N, replace this bit with the remainder, and carry (add the n-1 bit to 1)
2) Then we will process n-1 bits, N-2 bits,... until there is no carry until a new arrangement is produced after processing.
3) Remove the arrangement with the same elements
4) when the value of the first element is greater than N, it ends.
Take the arrangement of 3 numbers 1, 2, and 3 as an example: the original arrangement is 1 2 3, and from it, the first 3rd elements are 3, 3 + 2 = 5 mod 3 = 2, the first element is 2nd + 1 = 3, so the new arrangement is 1 3 2. The order generated by adding value to the element is: 1 2, 3, 1 3, 2, 1, 2, 2, 2, 3, 3, 3, 2, 2, 1
Duplicate elements exist in the underlined arrangement. If the elements are discarded, all elements are arranged.
6. Recursive Algorithms
The method for generating a full arrangement is concise in recursive mode, and there are also many implementation methods.
1) backtracking
The backtracking method is usually used to construct a spanning tree. Take three elements as an example. A tree node has data, which can be 1, 2, or 3. If a value is 0, it indicates that the value has not been set.
The initial state is (0, 0, 0). You can select 1, 2, and 3 for each element value. Therefore, three subnodes are extended. Find out the possible values of the 2nd elements of these nodes in the same way. If the three data of the new nodes are all non-zero, a full arrangement scheme is found. When you try all possible solutions, you can get answers to your questions.
2) Recursive Algorithms
If P is used to represent the arrangement of n elements, and PI is used to represent the arrangement of elements that do not contain I, and (I) PI is used to represent the arrangement of prefix I before the arrangement of Pi, then, the arrangement of n elements can be recursively defined:
If n = 1, P is arranged with only one element I
If n> 1, P is composed of (I) PI (I = 1, 2,..., n-1 ).
By definition, it is easy to see that if an arrangement of K-1 elements has been generated, then k elements can be arranged by adding element I before the arrangement pI of each K-1 element. For example, the arrangement of two elements is 1 2 and 2 1. For each element, P1 is 2 3 and 3 2, add 1 before each arrangement to generate a new arrangement of 1 2 3 and 1 3 2. P2 and P3 are 1 3, 3 1 and 1 2, 2 1, in the same way, 2 1 3, 2 3 1, 3 1 2, 3 2 1, and 3 2 1 are generated.
3) cyclic shift method
If an arrangement of K-1 elements has been generated, add element K after each arrangement to make it an arrangement of k elements, and then shift each arrangement loop left (right ), each movement generates a new arrangement.
For example, the arrangement of two elements is 1 2 and 2 1. Add 3 after 1 2 to form a new arrangement 1 2 3, and move it to the left of the cycle to form a new arrangement 2 3 1, 3 1 2, similarly, 2 1 can generate a new arrangement of 2 1 3, 1 3 2 and 3 2 1.
This article is from the csdn blog. For more information, see file: // D:/liuyanglong/algorithm, data Structure/-New/algorithm-New/fully arranged generation algorithm % 20-% 20 wake % 20up % 20-% 20csdn blog. MHT