{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

14. Sorting

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

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

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)

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

View Full Document
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
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)

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

View Full Document
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
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)

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.

{[ snackBarMessage ]}