{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture26 - Lecture 26 Recursion and Merge Sort Engineering...

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

View Full Document Right Arrow Icon
Engineering 101 Engineering 101 Lecture 26 Lecture 26 Recursion and Merge Sort Recursion and Merge Sort Prof. Michael Falk University of Michigan, College of Engineering
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
Announcements Announcements Project 6 (simulating cardiac tissue) is due Monday Nov 13, 9pm Exam 3 Monday Nov 20, 7-9pm Early Admin Friday Nov 17, 3-5pm
Background image of page 2
Recursion Recursion A program that calls itself is called recursive . Example: int factorial (int n){ if (n==0) return 1; else return n* factorial(n-1); }
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
Recursion Recursion factorial (4) return 4* factorial(3)
Background image of page 4
Recursion Recursion factorial (4) return 4* factorial(3) factorial (3) return 3* factorial(2)
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
Recursion Recursion factorial (4) return 4* factorial(3) factorial (3) return 3* factorial(2) factorial (2) return 2* factorial(1)
Background image of page 6
Recursion Recursion factorial (4) return 4* factorial(3) factorial (3) return 3* factorial(2) factorial (2) return 2* factorial(1) factorial (2) return 1* factorial(0)
Background image of page 7

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

View Full Document Right Arrow Icon
Recursion Recursion factorial (4) return 4* factorial(3) factorial (3) return 3* factorial(2) factorial (2) return 2* factorial(1) factorial (2) return 1* 1
Background image of page 8
Recursion Recursion factorial (4) return 4* factorial(3) factorial (3) return 3* factorial(2) factorial (2) return 2* 1
Background image of page 9

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

View Full Document Right Arrow Icon
Recursion Recursion factorial (4) return 4* factorial(3) factorial (3) return 3* factorial(2) factorial (2) return 2
Background image of page 10
Recursion Recursion factorial (4) return 4* factorial(3) factorial (3) return 3* 2
Background image of page 11

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

View Full Document Right Arrow Icon
Recursion Recursion factorial (4) return 4* factorial(3) factorial (3) return 6
Background image of page 12
Recursion Recursion factorial (4) return 4* 6
Background image of page 13

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

View Full Document Right Arrow Icon
Recursion Recursion 24
Background image of page 14
Exercise 1 Exercise 1 What will be output if the string “up down” is passed to the procedure? 1- upay downay 2- puay ownday 3- upay 4- puay downay ownday
Background image of page 15

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

View Full Document Right Arrow Icon
Exercise 1 Exercise 1 What will be output if the string “up down” is passed to the procedure? 1- upay downay 2- puay ownday 3- upay 4- puay downay ownday
Background image of page 16
Exercise 2 Exercise 2 void dothis (string text){ if (text.size()==0){ cout << endl; return; } int n = text.find(“ “); if (n==text.npos){ n=text.size(); cout << text.substr(1,n-1) << text.substr(0,1); cout << “ay” << endl; return; } cout << text.substr(1,n-1) << text.substr(0,1); cout << “ay “; dothis (text.substr(n+1,text.size()-n-1)); return; }
Background image of page 17

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

View Full Document Right Arrow Icon
Exercise 2 Exercise 2 find() locates a number in a sorted list. What parameters should be passed in the last call to find()? 1- list, mid+1, end, num 2- list, begin, mid+1, num 3- list, mid-1, end, num 4- list, begin, mid-1, num
Background image of page 18
Exercise 2 Exercise 2 find() locates a number in a sorted list. What parameters should be passed in the last call to find()? 1- list, mid+1, end, num 2- list, begin, mid+1, num 3- list, mid-1, end, num 4- list, begin, mid-1, num
Background image of page 19

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

View Full Document Right Arrow Icon
Example 3 Example 3 int find (const vector <int> & list, int begin, int end, int num){ int mid = (begin + end)/2; if (list[mid] == num) return mid; if (list[mid] < num) return find(list, mid+1, end, num); if (list[mid] > num) return find(list, begin, mid-1, num); }
Background image of page 20
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}