On recursive algorithm

Source: Internet
Author: User

Recursive algorithm

The programming technique called by the program itself is called recursion (recursion).
A process or function in its definition or description of the direct or indirect call itself a method, it usually transforms a large and complex problem layer into a small problem similar to the original problem to solve, recursive strategy requires only a small number of procedures can be described in the process of solving the problem of multiple repeated calculations, Greatly reduces the code volume of the program.
Attention:
(1) Recursion is the invocation of itself in a process or function;
(2) When using the incremental return strategy, there must be a clear recursive end condition, called a recursive exit.

A more classic description is the old monk tell the story, he said there was a mountain, there is a temple, there is an old monk in the temple to tell stories, he said there was a mountain, there is a temple in the mountains, there is an old monk in the temple in the story, he said there was a mountain, ... Repeat the story endlessly, until the last old monk bothered to stop.

Repeating a story can be seen as repeating itself, but assuming it doesn't stop, it doesn't make sense, so it's finally going to stop. The key to recursion is to find the recursive equation and the recursive termination condition. That is, the old monk repeated the story of this recursive equation to have, the last old monk bored to stop this recursive termination conditions also have to have.

Factorial algorithm can be defined as a function

When n>0, use F (n-1) to define F (n) and F (n-1-1) to define F (n-1) ..., which is a descriptive narrative of the recursive form.

When N=0, f (n) = 1, which is the condition of the end of recursion.

In fact, all recursion problems can be regarded as stratum problems.

The entire problem to be solved (the entire recursive function) is considered to be f (n). In this recursive function, for example, the following points:

Write a recursive exit.
To solve the problem that is currently being addressed-----quite the same as in the stratum problem (n)
The recursion goes down (calling itself) to solve the same but smaller problem-----equivalent to F (n-1)

Assuming that the function implements these points, then the recursive algorithm is basically successful.

Just a few questions his F (n-1) may have to be called several times (possibly with a different number of references), since he has to invoke F (n-1) at the time of implementation (n) as a precondition,

This is a very good example. This is the case compared to the Fanfanta problem.

All in all, you need to understand the transfer of complex recursive problems into a simple hierarchical recursion problem, when the problem is better understood and solved.

Here are a few examples of how to solve this problem recursively

A recursive algorithm is used to store the number of n in the array in reverse order.

The above method is used to analyze the procedure such as the following:

Package Sf.digui;

public class recursion{
private int b[]=null;
private int len=0;
public recursion (int b[]) {
This.b=b;
This.len=b.length;
}

public void Resevert (int i,int j) {
if (i>=j) return;
//====================
B[I]=B[I]+B[J];
b[j]=b[i]-b[j];//Note: There is no third party (another open memory) to implement the value exchange of two variables
B[I]=B[I]-B[J];
//=========================

Resevert (i+1,j-1);
}

public void Printthis () {

for (int i=0;i<len;i++) {
System.out.print (b[i]+ "");

}
System.out.println ();
}


public static void Main (string[] args) {
int b[]={1,2,3,4,5,6,7,8,9};
int len=b.length;
Recursion rec=new recursion (b);
System.out.println ("The number of start of array is:");
Rec.printthis ();
Rec.resevert (0,len-1);
System.out.println ("The number after the array has been reversed is:");
Rec.printthis ();
}
}

Two.. Complete with recursive algorithm: There are 52 cards, make them all face up, the first round is starting from the 2nd, where the cards in multiples of 2 are turned upside down, and the second round starts from the 3rd card, where the cards in multiples of 3 are turned upside-down and face-to-face upside-down, and the third round starts from the 4th card , all the cards in multiples of 4 are flipped on the same rules as above, and so on until the first card to turn over 52. The statistics finally have several cards facing up, along with their position numbers.

After analysis of the above methods, the procedure is drawn as follows:

Package Sf.digui;

public class digui{
private int n;
private int A;
private int p[]=null;//The positive and negative information of all cards
public Digui (int n) {
This.n=n;
A=n;
P=new Int[n];
for (int i=0;i<n;i++) {
p[i]=0;//here 0 means positive, 1 means negative
}
}


public void process (int a) {//====== equals f (n)
int b=a;
if (a==1) return;//The exit of the recursion
The following sections deal with what is currently to be done (to be able to think about things from the last or first step)---equivalent to (n)
//===================================================
while (b<=n) {//
p[b-1]= (p[b-1]==0)? 1:0;//
b=2*b;//
}
//====================================================
Process (A-1);//call itself, solve the same but small size of another problem---equivalent to f (n-1)


}

public void Printthis () {
Process (n);
for (int i=0;i<n;i++) {
System.out.println ("+ (i+1) +" Zhang's positive and negative serial number is: "+p[i]);
}
}


public static void Main (string[] args) {
Digui digui=new Digui (52);
Digui.printthis ();
}
}


/* Description:
* I think all recursion can see the same problem as the imaging class---equivalent to f (n) =n+f (n-1), all in the recursive function
* Solutions such as the following questions:
Write a recursive exit.
To solve the problem that is currently being addressed-----quite the same as in the stratum problem (n)
The recursion goes down (calling itself) to solve the same but smaller problem-----equivalent to F (n-1)
*
*
* To learn to transfer complex recursive problems with the same simple recursive problem as the imaging class

**/

The above is my study of some of the ideas of recursion, I hope that a lot of other people to reply, we come together to talk about, exchange, and common progress.

On recursive algorithm

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.