Illustration of recursive algorithm

Figure 1:

 

 Figure II:

 

 

 

Comparison of FIGS. I and II, their role is the same. From the analysis of the flow, a flow of FIG relatively complex, while FIG two simple,

which is by counterparts stackoverflow above remark: If using a loop, the performance of the program may be higher; if using recursion, the program may be easier death understanding. How to choose depends on what is important to you now.

So what is the difference between recursion and cycles is it?
Differences:
recursion is the function body calls itself, if not controlled, the endless calls itself until the stack overflows.
Loop is repeatedly executed the code region in a certain period, if not controlled, will form an endless loop.

The same point:
whether or recursive cycle, we must set certain conditions, or to end a recursive loop.

 

Recursive baseline conditions and conditions

When writing a recursive function, you must tell it when to stop recursively. Because of this, each recursive function has its two parts: the base portion and recursive conditions.
Refers to a condition recursive function calls themselves, the baseline condition is no longer refers to a function calls itself, thereby avoiding the formation of a wireless loop.

Code examples are as follows:

package cn.recursive.example;

public  class RecursiveExample {
    
    
      /**
       * A recursive method
       * @Param x
       * @return
       */
       public static int f(int x) {
           
           if (x == 0) {
               
               return 0;
           }
           
           return 2 * f(x - 1) + x * x;
       }
       
       
      

      
       public static void main(String[] args) {
           
    
         // call the method, when x = 2, is output. 6 
        the System. OUT .println (RecursiveExample.f ( 2 ));
          
    
       }
}

Return 0 corresponds to the baseline condition code, and 2 return F (. 1-X) + X X corresponds to recursive conditions.

Stack

A stack is a data structure that we used it, and we do not even know.

 

Although it is convenient to use the stack, but also a price to pay:
to store detailed information can take up a lot of memory. Each function call must occupy a certain memory, if the stack is high, it means that the computer stores information about a large number of function calls. In this case, usually the following two options:
(1) re-write the code, instead using a cyclic;
(2) a tail-recursive. This is an advanced recurrent theme. In addition, not all languages support tail recursion;

My feelings:
in fact, this chapter illustrates the recursive algorithm greatest feeling for me is to use a lot of pseudo-code and vivid images, so I think the code is really a dynamic guy vivid. In addition, I think the idea of pseudo-code to sort out the code of great help, especially to write a function of time, then you can write pseudo code comb logic, then started working on the validation logic pseudo-code is correct.

Guess you like

Origin www.cnblogs.com/youcong/p/10925334.html