Algo3Notes

Algo3Notes - We have introduced the recursion paradigm for...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
We have introduced the recursion paradigm for the binary tree data structure. Recursion is a general algorithm design approach, and here are additional examples. 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Factorial of n is the product of the first n numbers. Factorial of n can be computed recursively as factorial of n-1 times n. The termination condition is if n==1 then return 1. (Note that the == sign is used to emphasize that we test for equality between the left and the right quantities, and we do not assign 1 to n.) This is a termination condition because when it is true there is no more recursive call made. The chain of recursive calls is interrupted. The recursive call is the call to Factorial(n-1). The algorithm uses itself as a sub- algorithm. 2
Background image of page 2
Left: conventional (non-recursive) minimum algorithm. Right: recursive algorithm for finding minimum. The recursive algorithm is based on the idea that the minimum in an array is the smaller of: - The first element - The minimum of remaining elements MinR is first called as MinR(A, n, 0), since we want the minimum beginning from the first element, which has index 0. Then the minimum of elements 2 to n-1 is found by calling MinR recursively with arguments (A, n, 1) and saved into variable tmp. Finally, the overall minimum being returned is the smaller of the first element or tmp. MinR(A, n, 2) makes another recursive call, MinR(A, n, 3) and so on. When i0 becomes n-1, no more recursive call is made. “If i0 == n-1 then return A[i0]” is the termination condition. 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Here is a recursive sorting algorithm. The idea is to sort the array by splitting the array in half, sorting each half independently, and then merging the two sorted halves in the overall sorted algorithm. The recursion keeps splitting the array until the array has a single element. When l
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 28

Algo3Notes - We have introduced the recursion paradigm for...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online