Deep understanding of recursion: Full Arrangement

Source: Internet
Author: User
1. Question:

Enter a string to print all characters in the string.

For example, if the input string ABC is used, all strings that can be sorted by characters A, B, and C are output.

ABC, ACB, Bac, BCA, cab, and CBA.

Solution:

Select each element from the set, and use it as the first element in the arrangement. Then, the remaining elements are arranged in full order. In this way, all elements are arranged recursively. Taking ABC as an example, we can do this: Take ABC as an example.
Fix a and find the following BC: ABC, ACB. After the request is completed, a and B exchange to obtain the BAC.
Fix B and locate the following AC: BAC and BCA. After the request is completed, place C in the first position to obtain the CBA.
Fix C and find the order of the following BA: CBA and cab. The code can be written as follows:

 

void Permutation(char* pStr, char* pBegin);    void Permutation(char* pStr)  {        Permutation(pStr, pStr);  }    void Permutation(char* pStr, char* pBegin)  {      if(!pStr || !pBegin)          return;            if(*pBegin == '\0')      {          printf("%s\n", pStr);      }      else      {          for(char* pCh = pBegin; *pCh != '\0'; ++ pCh)          {              // swap pCh and pBegin              char temp = *pCh;              *pCh = *pBegin;              *pBegin = temp;                            Permutation(pStr, pBegin + 1);                // restore pCh and pBegin              temp = *pCh;              *pCh = *pBegin;              *pBegin = temp;          }      }  }  

"Every recursive call to a function is regarded as a simple operation. As long as the interfaces are consistent, you must be able to implement the functions defined in the Specification Description. Do not think too deeply or too far. Just as when the second mathematical induction method is used to prove a proposition, it is impossible to doubt whether the induction hypothesis is correct when the induction hypothesis is inductive proof ."

 

 

2. Description of the function call Mechanism


No nested definition is allowed between any function. The called function and the called function are independent of each other (they can be called ). When a function is called, The called function protects the running environment and return address of the called function, so that the status of the called function can be completely restored after the called function returns a result, the status is not related to the called function.
Although the Code executed by the called function is the code body of the same function, different points returned due to the call point and call status can be considered as a copy of the function, it has nothing to do with the code that calls the function, so the code of the function is independent. The stack space for running the called function is independent of the stack space for calling the function. Therefore, data between called functions is irrelevant. Functions are linked by parameter passing and return values. functions are black boxes.
This mechanism determines that C/C ++ allows recursive function calls.

 

References:

Http://blog.csdn.net/v_july_v/article/details/6879101

Http://www.cnblogs.com/seaven/archive/2010/12/17/1908953.html

 

Related Article

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.