Recursion and iteration)

Source: Internet
Author: User

When dealing with some complex problems, the two most common methods are recursion and stacking. They directly use the high-speed computing capability of computers to solve the problem. Both methods split a complex problem into smaller or even smaller steps that are easy to solve, but they are slightly different.

In both cases, the stacking method may be simpler. The stacked method breaks down the problem into a series of sub-steps, one by one. For example, if you need to accumulate all the natural numbers less than five, you need to first (step 1), then add two (step 2), and then add three (Step 3. Add different numbers to each step (the numbers are the same as the steps ). This is called "iterative traversal ".Program(Iterating through the problem, Translator's note ). Only a part of the step numbers from one step to another (the original version is step number, the Translator's note) are actually changed, because you can calculate all other information (such as the number you want to accumulate) from the step number ). This is the key to iteration: finding all other information through the step number. The typical example of iterative application in languages is the For Loop (Translator's note) Statement in basic or C ++.

If iteration solves a lot of steps one by one, recursion means to pile up all steps and solve them one by one. It looks like a mirror facing another one: each image is similar to another, but completely different.

It is better to use an example to explain. How do you let the computer know if a person is a descendant of Genghis Khan? UseAlgorithmTo define a person, or not a descendant of Genghis Khan, is like this:

This person is a descendant of Genghis Khan when and only when his/her father is Genghis Khan, or his/her father or mother is a descendant of Genghis Khan.

Wait, are we in violation of a familiar rule from an early age -- do not use words (maybe in this example) to define itself? Okay, Here we didn't use the complete loop logic, because we didn't say "a descendant of Genghis Khan is a descendant of Genghis Khan ". Let's extend this definition a little.

Jim. Although he did not know, he was the great-sun of Genghis Khan. Because Jim's father is not Genghis Khan, we must check whether his father is a descendant of Genghis Khan ...... so we applied this definition to Jim's father again, just as we did for Jim. Jim's father (his grandfather) was not Genghis Khan, so we went to see his father's father again (his great-grandfather ). Wait! Its great-grandfather was Genghis Khan. That means Jim's grandfather is a descendant of Genghis Khan, and his father is also a descendant of Genghis Khan.

Have you noticed that we are getting deeper and deeper into a problem and applying it in the same way until we reach something? This is brand new, that is, the "end ". Then, unless the problem has been solved, we will return the starting point of the problem in the order of "Expand. This recursive technology similar to "self-replication" also leads to a joke for programmers:

Recursion/smoke/: see recursion (here is a recursive logic statement to avoid smoke. The original Article is recursion/ree-ker '-zhon/: see recursion. Note by the translator)

Now we can see the difference between Recursion and iteration. For iteration, each step clearly guides the next step, as if stepping on the stone to cross the river, but for recursion, each step copies itself on a smaller scale, eventually they all work together to solve the problem. A complete understanding of these two methods is crucial because they are used in almost every computer algorithm.

Http://www.yeeyan.com/articles/view/14392/3982/dz

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.