Algorithm Series (15) the application of loop and recursion in the algorithm

Source: Internet
Author: User

I. The relationship between recursion and circulation

1, the definition of recursion

Sequential execution, cycle and jump are the three basic control structures of programming language in von Neumann computer system, these three kinds of control structures constitute the algorithm, program and even the whole software world. Recursion is also a program control structure, but it is generally considered not as a basic controlling structure, because the recursive structure can be replaced by a carefully designed cyclic structure in general, so it can be said that recursion is a special cyclic structure. Because recursive methods call their own algorithms directly or indirectly, they are a more powerful loop structure than iterative loops.

2, recursive and cyclic implementation of the differences

The loop (iterative cycle) structure is usually used to solve linear problems, such as polynomial summation, linear iterations for the precision of a given result, and so on. A typical loop structure typically consists of four components: the initialization section, the cyclic condition section, the Loop body part, and the iteration part. The following code is an example of using a looping structure to solve factorial:

86/* Cyclic algorithm calculates the factorial of a small number, 0 <= N < a
       
/1 int calcfactorial (int n),   
       
{a     .   
       
The     int i;   
       
A for     (i = 1; I <= n; i++) {The result = the result         * i;   
       
The return to result     ;   
       
98}

The recursive method is usually divided into two parts: the recursive relationship and the recursive termination condition (the solution of the minimum problem). The key of the recursive method is to determine the recursive definition and the recursive termination condition, the recursive definition is to solve the problem, and it is the rule that points to the recursive termination condition, and the recursive termination condition is usually the solution of the minimum problem. The recursive structure is similar to the human problem-solving method, the algorithm is simple and easy to understand, and it can describe the whole process of solving problems with fewer steps. The structure of the recursive method also implies a step, which is "backtracking", which is more efficient with recursive methods for operations that require an "advanced out" structure. The following code is an example of a recursive method for solving factorial:

100/* Recursive algorithm calculates the factorial of a small number, 0 <= N < a
       
/calcfactorial int (int n)   
       
102   
       
{     if (n = = 0)/* The solution of the smallest problem, that is, the recursive end (i) The conditions of return         1;   
       
The     calcfactorial return n * (n-1);/* Recursive definition */
       
107}

From the above two examples can be seen: recursive structure algorithm code structure simple and clear, readable, very consistent with the "code is the document" Software design philosophy. But the shortcoming of recursive method is also obvious: the operation efficiency is low, the storage space occupies more than the iterative loop method. Recursive method through nested call itself to achieve the purpose of the loop, the function call caused by the parameters of the stack and other costs will reduce the efficiency of the algorithm, the same, the occupancy of storage space is reflected in the stack parameters and local variables occupy the stack space. Because of these two points, the application of recursion method and the scale of problem solving are affected by the size of the system task or line stacks space, in some embedded systems, the task or thread stack space is only thousands of bytes, in the design algorithm should be cautious to use recursive structure algorithm, otherwise it can easily lead to stack overflow and system crashes.

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.