-
Recursion, in a nutshell, is when the application calls itself to query and access hierarchical data structures. The use of recursion can make ** more concise and clear and readable (not necessarily for beginners), but because recursion requires a system stack, the space consumption is much larger than that of non-recursive, and if the recursion depth is too large, the system resources may not be enough.
There is often a view that recursion can be used without recursion, and recursion can be replaced by iteration.
It is true that, in theory, recursion and iteration are equivalent in terms of time complexity (without considering the overhead of function calls and the stack overhead generated by function calls), but in practice recursion is indeed less efficient than iteration.
The existence of all things needs to be tested by time, and recursion has not been buried by history, that is, there is a reason for existence. Theoretically, all recursive functions can be converted into iterative functions and vice versa, but the cost is usually high. However, from the perspective of algorithm structure, the structure of recursive declarations cannot always be converted into iterative structures, because the extension of the structure itself belongs to the concept of recursion, which cannot be implemented at all in the early stage of design by iterative methods, just as dynamic polymorphic things can not always be implemented with static polymorphism.
This is also the reason why in structural design, the recursive approach is usually used instead of the iterative approach, a very typical example is similar to the linked list, the use of recursive definition is very simple, but for the memory definition (array mode) its definition and call processing instructions become very obscure, especially when encountering problems such as loops, graphs, grids, etc., the use of iterative methods from description to implementation becomes unrealistic. So in effect, all iterations can be converted to recursion, but recursion is not necessarily convertible to iterations.
The prerequisite for adopting a recursive algorithm is that the recursive algorithm can be used if and only if there is an expected convergence, otherwise, the recursive algorithm cannot be used.
Recursion is actually convenient for programmers, and recursion can be easily converted into programs through mathematical formulas. The advantage is that it is easy to understand and easy to program. However, recursion is implemented by a stack mechanism, and each layer goes deeper, it has to occupy a stack data area, and for some algorithms with deep nested layers, recursion will be powerless, and it will end up in memory crash spatially, and recursion also brings a large number of function calls, which also has many additional time overhead.
So when the depth is large, its spatiotemporal nature is not good.
While iterations are efficient, and the running time only increases due to the increase in the number of loops, there is no additional overhead and no increase in space, but the disadvantage is that it is not easy to understand and difficult to write complex problems.
Therefore, Enoch does not agree with the understanding that "if you can use recursion, you don't need recursion, and recursion can be replaced by iteration", Enoch does not dare to agree, or look at it dialectically, and cannot be beaten to death with a stick.
Resources. blog/cns!9cf795352e94bf70!
-
Read more books and write more programs.
The most classic textbook for the C language is Brian WThe C Programming Language, co-authored by Kernighan and Dennies. One of the authors (Dennies) is the father of the C language. >>>More
Scope. You static char *chh;
static char *ch1;Although the address pointed to by the two pointers does not change, have you ever wondered whether the memory address they point to has been released, char chc[10]; It's local, the function is out, the lifecycle is over, and you're trying to access it with a pointer in void times(). >>>More
Valid variable names for the C language:
First, it can only contain numbers, letters, and underscores. >>>More
These are not macro definitions, these are file containments. >>>More
Computer Level 2C language question types and scoring criteria: 1. Multiple choice questions: 40 questions, 1 point for each question. Points are given according to the standard answers. >>>More