According to the arrangement in the Yellow Book, it is time to talk about recursion. I checked it online. The article on recursion can be said to be "full of resources"-Please forgive me for being sour here. I mean, writing something smelly by others for everyone to read, it's just a waste of time, so my following things should be something that at least seems new to me. If you think something is unclear, see related articles (too many ). Even so, I still cannot write what I want to say in this article. It seems that I have a really bad habit.
I have read this question and asked, "Is sequence, selection, and loop necessary for the three basic structures of program structured design ?" Of course, you know that such an argument is enough as long as there are three types; but can there be fewer? The answer is "yes" because recursion can replace loops. For example, the following function is used to sum elements in an array:
Float rsum (float a [], const int N)
{
If (n <= 0) return 0;
Else return rsum (A, n-1) + A [n-1];
}
Actually:
Sum = 0;
For (INT I = 0; I <n; I ++) sum + = A [I];
However, the actual situation is that any language contains a circular structure, but not any language supports recursion. To apply a sentence, recursion is omnipotent, but there is no recursion. However, I have seen that some people now need recursion regardless of the problem. It is clear that loop is the first method to come up with, but they have to use their brains to find recursive algorithms. I really don't know what to say.
Is recursion an algorithm?
We often see "recursive algorithms" and "non-recursive algorithms". This method does not have semantic problems, and I also use -- Recursive Algorithms myself. But this also shows that recursion is not an algorithm. It is also an idea that is called a recursive algorithm because the guiding ideology of an algorithm is recursive; however, if you do not use recursion as the guiding ideology, the resulting algorithm is non-recursive. -- Recursive solutions are available for problems that can be processed by loops. In this sense, loop algorithms can all be called non-recursive algorithms.
I just want to let everyone know what kind of algorithms can be written. The key is the guiding ideology when you write algorithms. If you think of a method of loop and iteration at the beginning, you will bother looking for Recursive Algorithms-even if you find a seemingly "concise" algorithm, because of his inefficiency, it is actually waste-what are you still doing this kind of useless work? Typical study habits. If you only think of recursive methods, and now you want to use stacks to eliminate recursion, you only do what the system does, and how much efficiency can you improve? Blind superstitious dissolution of recursion will certainly improve efficiency without justification-the method of your work, if not, is not even as good as the system's original practice.
From the day I learned to arrange and combine, the factorial I know is like n! = 1 × 2 × ...... N. If I want to write a factorial algorithm, I only want to multiply from 1 to n. Another example is the Fibonacci series. If someone describes it in a natural language, it must be like this. The first two items are 0 and 1, and each item in the future is the sum of the first two items. Therefore, I can only get the iteration solution "Save the first two items and then add the results. -- Now, as long as we talk about recursion, there will be their debut. Its name is: "The definition is recursive, So we write a recursive algorithm ". Where does the defined recursive abstraction come from? Obviously, the concept of factorial is abstracted from a cyclic process, and the definition of the Fibonacci series is an iterative abstraction. Therefore, we first extract a recursive definition from a fact that is not recursive, And then we say, "because the definition of the problem is recursive, we can easily write a recursive algorithm ", then, "We can also convert this recursive algorithm into a loop or iterative algorithm." It makes people feel like 1 Gbit/s 3 = 0.33 ......, 0.33 ...... × 3 = 0.99 ......, Then we spent a lot of time to understand that 1 = 0.99 .......
There are still some people who are happy to be happy. It is because when we talk about recursion, we need to mention these two. As a result, no student can see the definition of factorial, none of them admire the recursive factorial function? Therefore, a convincing example is required to introduce recursion, which is beyond the name of qingta.