16. Full arrangement (Detailed records of deep search)

Source: Internet
Author: User

The full line of code is collected and researched on the internet, including my own writing and looping implementations. Loops are the easiest to understand and are nested according to criteria, but the downside is that if there are 10 eight data, the number of layers nested in the loop is too deep and bloated. Obviously, if a piece of code is not concise enough, I am not satisfied with it, I must try my best to solve it. So let's record the full-array recursive implementation again.

or according to your own set of methods, Sprat, do not get too much data, in the smallest extent to observe how the code is running. In a fully arranged recursive implementation, three arrays are required:
1. The source data, which is the element to be queued, for example, {3,5,7,8 ...} and so on, put it in an array.
2. The data is in good line. The row of data here refers to the need to extract a few, if the source data to extract 2 data arrangement, the array of this arrangement is set to 2, because only 2 data is stored.
3. The state array, this array and the source data array is long, because, if there are n data, each data can be selected, so long as the source data array, the entire recursive process depends on the state array to work.
The code is as follows:

#include <iostream>using namespacestd;inta[3];/*array for storing permutations*/intflag[3];/*State Array*/intb[4]={0,3,5,8};/*Source Data*/intn=3;/*number of source data: 3*/voidFintm) {if(m==3)/*Allocation Complete*/{     for(intj=1;j<3; j + +)/*Print*/cout<<A[j]; cout<<Endl;}Else for(intI=1; i<=n;i++)    if(flag[i]==0) {A[m]=B[i]; Flag[i]=1; F (M+1); Flag[i]=0; }}intMain () {f (1);return 0;}
View Code

The

Now analyzes this code in detail: ( all subscripts start from 1 for easy analysis of )
Why there is a for loop in code because, If there is n data, according to the common sense of arrangement, each data can be selected, if the first element is selected 3, then the next 5 and 8 have the same opportunity to be selected in the first number of permutations.
for the first time, M=1 enters, passes through the loop, I=1, and then calls F (2), which is the key to remembering I this value, where it now (stays) = 1.
The second time, m=2 into, through the cycle, I=2, then set the flag[i]=0 in the state array, that is, replace 5 , and then loop, take the source data of the 3rd data, and then recursive to the boundary, then print the array, get 3,8 ...

If there is data behind the source data, for example, 9,10 .... And so on, then this routine will continue, select one, discard one, and then try the next, so repeatedly, always poor to the last data of the source data.

If exhaustive, and then back to F (1), all the data it has selected will be set to 0, which is easy to see. That is, when you return to F (1), the state array is: 1 0 0.
This means that after the first data is determined, all the possible of the next data that is exhaustive is the model: if you want to select 2 elements, the first to be exhaustive is probably the last of its kind, then push forward. The
Model 1 illustrates this problem, (in fact, a lot of strange inspiration is in the blog when unconsciously captured, so writing is very important!) Just like this, after drawing, it suddenly feels like this recursive and for loop is very similar! Just follow the idea of A For loop:

 /*   for  (I=1 ; i<=n;i++ for  (J=1 ; j<=n;j++ for   (...) .....}

That is, the first element is selected, the inner loop is nested inside, then the second number, the third number ... And so on, the difference is that the for loop is constrained by internal decision conditions, for example, the second is not equal to the first, the third is not equal to the second and the first ... So it's often said that recursion and loops can be transformed into one another!
According to such an analysis, it is quite clear, at least know how the model works. Now this example takes only two numbers, reduces the range of the code a lot, and sees it clearly, if the data is large, the reasoning is the same as this type.
For example:
source Data ={3,5,7,8,9,6}, choose three elements to arrange, according to the analysis, its process is:
The first one, the second one, then the third one, get: 3,5,7 | 3,5,8|3,5,9|3,5,6|, the third poor lift is complete.
Back to the second, because already took 5, the next turn to 7, then, then the poor lift third, get: 3,7,5|3,7,8|3,7,9| .... The third poor lift is complete. Go back to the second one, and then the third one is poor.
Wait until the second exhaustive completely , and then back to the first, turn 5 is the first, then determine the second, and then the poor third, repeatedly, and so on, the final completion of the entire arrangement process!

Based on the above analysis, it is finally shown how to convert in recursion and loop, first with the For loop nesting as an example:

 for (..) {    for(...)    {        for(..)    .}

This is a common loop nesting, one layer at a layer, how to translate into recursion, according to the above example, it is easy to see, as long as the recursive function has a for loop in the line, so that the loop nested compression into a recursive form:

void f () {        ..          for ( )        { }..}

The two are equivalent! It seems that the basic knowledge is not flexible enough to grasp!
The end of the for loop is judged at the front of the recursive function.
From this figure can be seen, the original recursion just on the surface to do a little "confused", do a little word game!

This reminds me of the first small algorithm recorded, 99 multiplication table, the nature is very similar, first recursive separation of 9,8,7, .... 1, then take the end of the 1 recursion, print multiplication, that is, the for loop separated out to make recursion!
I see a lot of code on the Internet, but rarely willing to elaborate, at least this diary detailed analysis of its process!

The basics tend to look less careful, but the idea of actually understanding it, and the flexibility to use it, requires painstaking exercise! So: knowledge is not the size of the high and low points !

16. Full arrangement (Detailed records of deep search)

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.