Recursion of programming ideology and recursion of programming ideology

Source: Internet
Author: User

Recursion of programming ideology and recursion of programming ideology


I have previously written a blog post on recursive algorithms, but as a series of programming ideas, I have to further analyze them. It is a simple, common, and important programming idea.

What is recursion?

For example:

There is an 8-weight apple that requires you to cut into several equal parts. The weight of each part cannot exceed 1. You will surely think of this:

1. First, cut an apple into two equal parts A1 and A2;

2. Cut one A1 into two equal parts, A11 and A12, and A21 and A22, respectively;

3. Cut A11 into two equal copies ......

4. Until each small part is less than or equal to one.

The above example is a recursive model that converts a large thing into a number of small things, each using the same method.

 

More professional definition:

The program itself calls its own programming skills calledRecursion(Recursion ). Direct and indirect Recursion

• Direct recursion: the function itself is called during execution.

• Indirect recursion: When a function is executed, other functions are called before they are called.

Recursion has four features:

1. There must be conditions for final termination, otherwise the program will be in an infinite loop;

2. The subproblem is smaller in size than the original problem, or closer to the termination condition;

3. subproblems can be solved through recursive call again or directly because the termination conditions are met;

4. The subproblem solution can be combined into the whole problem solution.

 

The above example also satisfies the above four properties:

(1 ). the condition for termination is that the weight of each part cannot exceed 1 or 2 ). the size of each cut is smaller than that of the previous one. (3 ). the method of each cut is the same, so sub-problems can be called recursively; (4 ). each small part is the required solution.

 

Implementation of the above example:

Public static void sliceApple (float weight, int times) {if (weight <= 1) {// System. out. println ("weight:" + weight);} else {float w = weight/2; System. out. println ("Number" + times + "times equals weight:" + w + "" + w); times + = 1; sliceApple (w, times ); sliceApple (w, times) ;}} public static void main (String args []) {sliceApple (8, 1 );}

Result:

The weight of 1st equals points is: 4.0 4.0

The weight of 2nd equals points is: 2.0 2.0

The weight of 3rd equals points is: 1.0 1.0

The weight of 3rd equals points is: 1.0 1.0

The weight of 2nd equals points is: 2.0 2.0

The weight of 3rd equals points is: 1.0 1.0

The weight of 3rd equals points is: 1.0 1.0


What can recursion do?

Break down big problems into small problems and simplify complicated problems;

Make the program easier to understand and enhance readability;

How do you use recursion?

Analyze the problem to see if the problem belongs to a recursive model and whether it can be solved using a recursive model. Whether or not a recursive model can be used depends on whether it meets the four characteristics of recursion.

Recursion saves the information of the call point during the call, so there is a call overhead. When there are high requirements on efficiency, it is better not to use recursion if you can solve the problem with loops, because loops do not have the call overhead, and the efficiency will be higher.

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.