{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

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

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

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

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

View Full Document
Recursion Recursion factorial (4) return 4* factorial(3)
Recursion Recursion factorial (4) return 4* factorial(3) factorial (3) return 3* factorial(2)

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

View Full Document
Recursion Recursion factorial (4) return 4* factorial(3) factorial (3) return 3* factorial(2) factorial (2) return 2* factorial(1)
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)

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

View Full Document
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
Recursion Recursion factorial (4) return 4* factorial(3) factorial (3) return 3* factorial(2) factorial (2) return 2* 1

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

View Full Document
Recursion Recursion factorial (4) return 4* factorial(3) factorial (3) return 3* factorial(2) factorial (2) return 2
Recursion Recursion factorial (4) return 4* factorial(3) factorial (3) return 3* 2

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

View Full Document
Recursion Recursion factorial (4) return 4* factorial(3) factorial (3) return 6
Recursion Recursion factorial (4) return 4* 6

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

View Full Document
Recursion Recursion 24
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

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

View Full Document
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
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; }

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

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

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

View Full Document
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); }
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}