{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

14. Sorting - ENGR 101 Section 100 Fall 10 Recursion and...

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

View Full Document Right Arrow Icon
ENGR 101, Section 100, Fall 10 M. Wellman 1 Recursion and Sorting ENGR 101, Lecture 14: 1 Nov 10 Announcements Exam 2 on Wed 3 Nov (6pm) watch for room announcements Open book/notes, same style/rules as Exam 1 Samples posted, review in Wed lecture Project 6 due Wed 10 Nov (9pm) last C++ assignment for the term (two more programming projects in Matlab)
Background image of page 1

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

View Full Document Right Arrow Icon
ENGR 101, Section 100, Fall 10 M. Wellman 2 Factorial n ! = 1 if n = 0 n ( n – 1)! otherwise Recursive definition Relatives x is a relative of y iff: x is y ’s parent x is y ’s child x is y ’s spouse x is a relative of a relative of y Recursive definition
Background image of page 2
ENGR 101, Section 100, Fall 10 M. Wellman 3 Cousins x and y are first cousins iff they share a grandparent x and y are Nth cousins iff their respective parents are (N–1)st cousins What are siblings? Recursive Definitions General Definition of a concept refers to the concept itself Programming Definition of a procedure invokes the procedure itself (ditto for functions)
Background image of page 3

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

View Full Document Right Arrow Icon
ENGR 101, Section 100, Fall 10 M. Wellman 4 “Divide and Conquer” A common algorithmic approach Three steps: Divide : Split problem into subproblems. Conquer : Solve subproblems. Combine : Merge subproblem solutions into overall solution. Recursive : Solution to problem based on solution to subproblems Back to Factorial Can write a function directly corresponding to mathematical definition: int factorial (int n) { if (n == 0) return 1; else return n * factorial(n-1); } n ! = 1 if n = 0 n ( n – 1)! otherwise
Background image of page 4
ENGR 101, Section 100, Fall 10 M. Wellman 5 Tracing Factorial factorial (4) return 4 * factorial (3) factorial (3) return 3 * factorial (2) factorial (2) return 2 * factorial (1) factorial (1) return 1 * factorial(0)
Background image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}