Get to the bottom of the difference between recursion and iteration, and examples of comparative advantages and disadvantages

Iteration is people, recursion is God!

From "Beauty of Programming" point of view, you can borrow a very classic: "! Iterations are people, recursion is God," to be sure of two from the macro.

Conceptually, the recursive procedure calls itself refers to a programming idea that a function call itself; iteration variables with known values, evolving variable according to the recurrence formula to get new programming ideas worth.

Recursion

Recursive function that calls itself.

Recursive configuration conditions should have,:

  1. The child must issue the original problem is the same thing, and more simple;

  2. Do not unlimited calling itself, must have an outlet that simplify the situation for non-recursive processing.

Example:

 

Resolution:

 

   These data when the program is running, the call letters do come at a price, that is, to take up a place called stack (stacky memory space), when you call the function, you must put some data into the stack when the function ends run will be removed from the stack, we can imagine, if you call a lot of functions, but these functions do not return, the stack was filled, no place to put the data, this is called a stack overflow error in terms of running . This is a fatal error, so the program will be forced to suspend the operating system.

In Python, artificially set number of times (depth) of the recursive calls, try to ensure that the program does not crash.

 

Iteration

int fib(int n){  
    int i, temp0, temp1, temp2;        
    if(n<=1) return n;  
    temp1 = 0;  
    temp2 = 1;  
    for(i = 2; i <= n; i++){  
        temp0 = temp1 + temp2;  
        temp2 = temp1;  
        temp1 = temp0;  
    }  
    return temp0;  
}

 

  Iteration and the difference between the ordinary cycle is: iteration, the variable loop code is also involved in computing a variable to hold the result, currently stored as the initial value of the next cycle of calculation.

  The difference between ordinary recursive loop is: loop is never to return, and recursion is there to go back there (because of the termination condition).

  In a large number of cycles of time, significantly higher than the recursive iteration.

method

  Iterative differently, if there is a product requires six months of delivery, I will come to in the first month of a product, of course, this product will be far from perfect, there will be a lot of features not yet added to it, many bug, is not stable, but after watching customers, present a more detailed amendments, so you know how far away the customer's needs, then I go home, spend a month in the last month made needs analysis, the basic framework design, code, test, and so on, to further improve, has come up with a better product, and give the customer to see, let them put forward their views.
So, my product in function, can gradually approaching the requirements of customers on quality, not after I spent a lot of effort, only until the last publication found that the situation is not what the customer wants.

Advantage

This process is much like playing a game, you just started playing, and it would lose, but as more and more times you play, your skills getting superb, and this results in you not start playing when planning out, but in the process you play through continuous output continued to lose, out of practice.

So, you can reach the result, you are in the process of doing it, trial and error, constantly adjusting, continued to improve, a natural final result obtained.

So, we can not be simply interpreted as iterative "upgrade."

Upgrade, more description is a result of a direct, one-time, a goal reached is a linear process.

Iteratively, through countless times, constant, repetitive, approaching a target, and back close, and then folded again approaching, and ultimately achieve the goal. It's not one process, through repeated, but a little better off than before each repetition of such a non-linear process.

 

Now we have "iteration" of dismantling it Keywords:

1. Repeat:

Constantly repeated, not a one-time completion.

2. Improved

Continuous improvement in the process of doing, adjust and optimize.

3. Cognitive upgrade

Iterative process is to constantly improve the process of cognition, upgrading is just a result of this process.

Guess you like

Origin www.cnblogs.com/xiaozhongfeixiang/p/11520161.html