{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Recursion1.5

# Recursion1.5 - Last Updated 10:12 AM CSE 2011 Prof J Elder...

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

Last Updated 12-01-24 10:12 AM CSE 2011 Prof. J. Elder - 26 - Example: Iteratively Reversing an Array Algorithm IterativeReverseArray( A, i, j ): Input: An array A and nonnegative integer indices i and j Output: The reversal of the elements in A starting at index i and ending at j while i < j do Swap A [ i ] and A [ j ] i = i + 1 j = j - 1 return

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

View Full Document
Last Updated 12-01-24 10:12 AM CSE 2011 Prof. J. Elder - 27 - Defining Arguments for Recursion Solving a problem recursively sometimes requires passing additional parameters. ReverseArray is a good example: although we might initially think of passing only the array A as a parameter at the top level, lower levels need to know where in the array they are operating. Thus the recursive interface is ReverseArray(A, i, j) . We then invoke the method at the highest level with the message ReverseArray(A, 1, n) .

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Last Updated 12-01-24 10:12 AM CSE 2011 Prof. J. Elder - 28 - Binary Recursion • Binary recursion occurs whenever there are two recursive calls for each non-base case. • Example 1: The Fibonacci Sequence Last Updated 12-01-24 10:12 AM CSE 2011 Prof. J. Elder - 29 - The Fibonacci Sequence • Fibonacci numbers are defined recursively: F = 0 F 1 = 1 F i = F i-1 + F i-2 for i > 1. (The “ Golden Ratio ”) Fibonacci (c. 1170 - c. 1250) (aka Leonardo of Pisa) The ratio F i / F i ! 1 converges to " = 1 + 5 2 = 1.61803398874989. .. Last Updated 12-01-24 10:12 AM CSE 2011 Prof. J. Elder - 30 - The Golden Ratio • Two quantities are in the golden ratio if the ratio of the sum of the quantities to the larger quantity is equal to the ratio of the larger quantity to the smaller one. ! is the unique positive solution to = a + b a = a b ....
View Full Document

{[ snackBarMessage ]}

### Page1 / 5

Recursion1.5 - Last Updated 10:12 AM CSE 2011 Prof J Elder...

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

View Full Document
Ask a homework question - tutors are online