Recursion1.5 - Last Updated 12-01-24 10:12 AM CSE 2011...

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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) .
Background image of page 2
Background image of page 3

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

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5
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 &gt; 1. (The Golden Ratio ) Fibonacci (c. 1170 - c. 1250) (aka Leonardo of Pisa) The ratio F i / F i ! 1 converges to &quot; = 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

This note was uploaded on 02/14/2012 for the course CSE 2011Z taught by Professor Elder during the Fall '11 term at York University.

Page1 / 5

Recursion1.5 - Last Updated 12-01-24 10:12 AM CSE 2011...

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