answersLogoWhite

0

Recursion is important because many algorithms are naturally recursive, particularly those that involve a divide-and-conquer technique. That is, we divide a complex problem into smaller instances of the same problem. Each division divides and reduces the problem further until the divisions are small enough to be resolved. We then aggregate those individual solutions to solve the original problem.

As a simple example, factorials can be calculated such that f(n) = n * f(n-1) for all n>1 where f(1) and f(0) are both 1. We can implement this function recursively as follows:

unsigned f(unsigned n) {

return n<2 ? 1 : n * f(n-1);

}

While the function itself is naturally recursive, that doesn't mean we must use a recursive solution. In this case, an iterative solution would actually be more efficient:

unsigned f(unsigned n) {

unsigned a = 1;

while (1<n) a *= n--;

return a;

}

However, in languages that allow compile-time computation (such as C++), recursive functions can be advantageous:

constexpr unsigned f(unsigned n) {

return n<2 ? 1 : n * f(n-1);

}

With this function, a good compiler will convert f(6) to the literal constant 720, completely eliminating the runtime overhead of invoking the function recursively. However, for values that cannot be computed at compile time due to excessive recursions, the runtime function will still be invoked, such that f(10) might be replaced with the constant expression 10 * f(9).

Languages that support template metaprogramming can provide the means to completely eliminate the runtime overhead associated with recursion:

template <unsigned N>

constexpr unsigned f () {

return N*f<N-1>();

}

template<>

constexpr unsigned f<1>() {

return 1;

}

template<>

constexpr unsigned f<0>() {

return 1;

}

Note that the terminating conditions are handled through specialisation rather than through recursion. At compile time, f<1>() invokes the first specialisation while f<0>() invokes the second. For all values N>1, the general function is invoked recursively at compile time.

constexpr unsigned x = f<10>();

Compile-time computation will effectively replace the above expression with:

constexpr unsigned x = 3628800;

Note that no code is generated for these template functions so we cannot invoke them at runtime, they are used purely for compile-time computation.

In languages that do not support template metaprogramming or constexpr, we can still make use of recursion, particularly where an iterative approach is too complex to implement efficiently. Divide-and-conquer algorithms are a typical example.

The Quicksort algorithm is a relatively simple recursive algorithm that can be implemented as follows:

void qsort (int a[], int lo, int hi) {

if (hi<=lo) return;

unsigned pivot = partition (a, lo, hi);

qsort (a, lo, pivot-1);

qsort (a, pivot+1, hi);

}

The partition() function does most of the actual work. Given the lower and upper bounds of the array it will select a pivot value and position this value such that all values to the left of it are less than it and all those to the right are not less than it. Once the array is partitioned, the index of the pivot value is returned. This value is used to determine the upper bound of the left sub-array (pivot-1) and the lower bound of the right sub-array (pivot+1).

User Avatar

Wiki User

9y ago

What else can I help you with?

Related Questions

What has the author Mitchell Wand written?

Mitchell Wand has written: 'Induction, recursion, and programming' -- subject(s): Computer programming, Induction (Mathematics), Recursion theory


What has the author Wim H Hesselink written?

Wim H. Hesselink has written: 'Programs, Recursion and Unbounded Choice (Cambridge Tracts in Theoretical Computer Science)' 'Programs, recursion, and unbounded choice' -- subject(s): Computer programming


What is the importance of programming?

Without programming, computers would be expensive doorstops. Computer hardware requires computer software. Programming, even in machine code, is essential in creating that software.


What type of science is recursion?

Recursion is a computer science. Typically computer programers write a specific program to test out a theory of recursion based on the known facts to try to define the variable.


What are the importance of programming?

Programming is what makes a computer more than just a simple pocket calculator. Everything that you can do today on a computer someone had to be programmed at one point.


What is the use of recursion function?

Read the part in your programming manual/text book about recursion. The short answer while easy does not tell you anything about the power or dangers of recursion. It is the power and dangers of recursion that is important because once understood you can use recursion to good effect without running out of stack space.


What is the difference between left recursion and right recursion in a grammar?

Recursion is what it's called when a function calls itself. When a function calls itself immediately before returning, it's called tail recursion. Tail recursion can be more efficiently written as iteration. In fact a good compiler will recognize tail recursion and compile it as iteration. There is no such thing as left or right recursion in C programming.


What is funcation?

Read the part in your programming manual/text book about recursion. The short answer while easy does not tell you anything about the power or dangers of recursion. It is the power and dangers of recursion that is important because once understood you can use recursion to good effect without running out of stack space.


How is recursion used in computer science?

Recursion is the process where one large problem is solved by solving several smaller problems. This applies to computer science, as trial and error and bug fixes are needed to get a finished product.


What are the merits and demerits of recursion?

Ans: Merits of recursion are: Mathematical functions, such as Fibonacci series generation can be easily implemented using recursion as compared to iteration technique. Demerits of recursion are: Many programming languages do not support recursion; hence, recursive mathematical function is implemented using iterative methods. Even though mathematical functions can be easily implemented using recursion, it is always at the cost of execution time and memory space. The recursive programs take considerably more storage and take more time during processing.


What are demerits of recursion?

Demerits of recursion are: Many programming languages do not support recursion; hence, recursive mathematical function is implemented using iterative methods. Even though mathematical functions can be easily implemented using recursion, it is always at the cost of execution time and memory space. The recursive programs take considerably more storage and take more time during processing.


Did you mean recursion?

By definition, recursion means the repeated application of a recursive definition or procedure. It is used to define an object in terms of itself in computer science and mathematical logic.