Recursive invocation Instance Analysis 2. Recursion in the middle of a function and recursion at the end of a function

Source: Internet
Author: User

The last article gave the recursive completion of the string inverse of the code, but did not analyze its specific algorithm, today, as the ' ABCDE ' string recursive flip diagram to share with you (the picture is rotten, concrete ideas are still some, details see annex)

The recursive call here does not appear at the end of the function, and two preceding recursion appear at the end of the function, so recursion can be divided into recursion at the end of the function and recursion in the function. The difference is as follows.

1. Recursion at the end of a function can be done in a circular way, so that most cases of recursion can only result in the brevity of the code, which is not conducive to the operation of the machine.

2. Recursion in the middle of a function does not have to be done in a circular way, so that recursion can be translated into recursion at the end of the function, and some do not. In the two forms of code written in the previous article, the recursion in the middle of the function is converted to recursion at the end of the function.

3. Recursive calls are used in binary tree operations, and recursion in a function can result in inefficient procedures, such as the solution of Fibonacci numbers (A1=1,a2=1,an=an-1+an-2 ...). The implementation code is as follows:

Int fib (int n) {         int result;          int pre_result;          int next_older_result;        result = pre_result  = 1;         while  (n > 2)          {                  n -= 1;                 next_older_result = pre_result;                 pre_result = result;                 result =  Pre_result + next_older_result;        }          return result;}

If you make n=50, the machine will run very slowly.

However, some algorithms are defined recursively, such as the factorial implementation code for a number, as follows

int factorial (int n) {int result = 1;                 while (n > 1) {result *= n;        n-= 1; } return result;

This greatly simplifies the operation.

In short, recursive calls need to be cautious, sometimes loops are simpler than recursion, and recursion is a thought-out and a simpler loop.

If there is enough hope to criticize

This article is from the "Pawnsir It Road" blog, so be sure to keep this source http://10743407.blog.51cto.com/10733407/1710972

Recursive invocation Instance Analysis 2. Recursion in the middle of a function and recursion at the end of a function

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.