Lecture+18

# Lecture+18 - Engineering 101 Engineering 101 Lecture 18...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Engineering 101 Lecture 18 11/6/07 Sorting and Recursion Quote of the Day Quote of the Day When one door to happiness closes, another opens; but often we look so long at the closed door ­ Helen Keller that we do not see the one which has been opened for us. Upcoming Events Upcoming Events Project 5 due tomorrow (11/7) at 9pm Project 6 released on Thursday (due Mon 11/19) Assigned teams will be posted online Contact your team member early to get started Michigan Lecture Competition – Phase 3 Friday 11/9 – contact your teams ASAP Winning section slides will be posted online this time Please rate them when you look at them More Upcoming Events More Upcoming Events Exam 3 – Wed. 11/14, 7:30p­9:30p New rooms EECS 1200 – Last names: Abe through Den EECS 1303 – Last names: Des through Koc EECS 1311 – Last names: Kra through Rao EECS 1500 – Last names: Red through Zha Sorting Sorting Often we have a list of numbers and we would like to order them. What is a good way to do this using a computer? Bubble Sort Bubble Sort Go through the list and make sure to move the highest to the end. Then go through the list again and make sure the second highest is second to the end. Rinse and Repeat… Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 5 0 3 4 1 6 5 2 Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 5 0 3 4 1 6 5 2 Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 5 0 2 4 1 6 5 3 Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 5 1 2 4 1 6 5 3 Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 5 1 2 3 1 6 5 4 Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 5 2 2 3 1 6 5 4 Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? intj; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 5 3 2 3 1 6 5 4 Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 5 3 2 3 1 4 5 6 Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 5 4 2 3 1 4 5 6 Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 4 0 2 3 1 4 5 6 Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 4 1 2 3 1 4 5 6 Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 4 2 2 3 1 4 5 6 Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 4 3 2 3 1 4 5 6 Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 3 0 2 3 1 4 5 6 Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 3 1 2 3 1 4 5 6 Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 3 2 2 3 1 4 5 6 Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 2 0 2 3 1 4 5 6 Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 2 0 1 3 2 4 5 6 Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 2 1 1 3 2 4 5 6 Bubble Sort Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } i j list[0] list[1] list[2] list[3] list[4] list[5] 2 1 1 2 3 4 5 6 News Flash! The Google phone is here! And it’s not a phone…it’s Android. http://www.youtube.com/watch?v=jWtFeIw8MVM http://www.youtube.com/watch?v=6rYozIZOgDk 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); } Recursion Recursion factorial (4) return 4* factorial(3) Recursion Recursion factorial (4) return 4* factorial(3) factorial (3) return 3* factorial(2) 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 (2) factorial(1) return 1* factorial(0) Recursion Recursion factorial (4) return 4* factorial(3) factorial (3) return 3* factorial(2) factorial (2) return 2* factorial (2) factorial(1) return 1* 1 Recursion Recursion factorial (4) return 4* factorial(3) factorial (3) return 3* factorial(2) factorial (2) return 2* 1 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 Recursion Recursion factorial (4) return 4* factorial(3) factorial (3) return 6 Recursion Recursion factorial (4) return 4* 6 Recursion Recursion 24 Which function will return the nth number in the Fibonacci sequence? 1 1 2 3 5 8 13 21 … Fn = Fn­1 + Fn­2 Exercise Exercise Exercise Exercise Fibonacci sequence: 3. int fib(int n){ if(n<=2) return 1; else return fib(n­1)+fib(n­2); } Making Bubble Sort Recursive… Making Bubble Sort Recursive… void bubblesort( vector<int> & list){ int i; // Up to which have we sorted? int j; // Which are we comparing? for(i=list.size( )­1; i > 0; i=i­1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; } Recursive Bubble Sort Recursive Bubble Sort void bubblesort( vector<int> & list, int i){ // i = Up to which have we sorted? int j; // Which are we comparing? if (i ==1) return; for(j=0; j < i­1; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); bubblesort(list, i­1) return; } 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 Exercise 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 Exercise Another Sort of Sort Another Sort of Sort What if we write a procedure that will merge two sorted lists into a combined sorted list? 1 What if we write a procedure that will merge 2 two sorted lists into a combined sorted list? 3 4 5 2 1 6 3 5 7 4 6 8 7 8 9 9 10 10 Another Sort of Sort Another Sort of Sort template<class T> void merge( const T & a, const T & b, T & c){ c.resize(a.size() + b.size() ); int aindex=0, bindex=0; for( int i=0; i<c.size(); i=i+1) if(aindex==a.size()){ c[ i ] = b[bindex]; bindex = bindex + 1; }else if (bindex==b.size()){ c[ i ] = a[aindex]; aindex = aindex + 1; }else if (a[aindex] < b[bindex]){ c[ i ] = a[aindex]; aindex = aindex + 1; } else { c[ i ] = b[bindex]; bindex = bindex + 1; } } Merge Merge a 2 3 4 7 9 b 1 5 6 8 10 template<class T> void merge( const T & a, const T & b, T & c){ c.resize(a.size() + b.size() ); int aindex=0, bindex=0; First create a new for( int i=0; i<c.size(); i=i+1) vector that is the if(aindex==a.size()){ size of the other c[ i ] = b[bindex]; two combined bindex = bindex + 1; }else if (bindex==b.size()){ c[ i ] = a[aindex]; aindex = aindex + 1; }else if (a[aindex] < b[bindex]){ c[ i ] = a[aindex]; aindex = aindex + 1; } else { c[ i ] = b[bindex]; bindex = bindex + 1; } } Merge Merge c a 2 3 4 7 9 b 1 5 6 8 10 template<class T> void merge( const T & a, const T & b, T & c){ c.resize(a.size() + b.size() ); aindex and bindex int aindex=0, bindex=0; for( int i=0; i<c.size(); i=i+1) will track the if(aindex==a.size()){ progress through c[ i ] = b[bindex]; the two lists bindex = bindex + 1; a and b }else if (bindex==b.size()){ c[ i ] = a[aindex]; aindex = aindex + 1; }else if (a[aindex] < b[bindex]){ c[ i ] = a[aindex]; aindex = aindex + 1; } else { c[ i ] = b[bindex]; bindex = bindex + 1; } } Merge Merge c a 2 3 4 7 9 b 1 5 6 8 10 template<class T> void merge( const T & a, const T & b, T & c){ c.resize(a.size() + b.size() ); loop over each int aindex=0, bindex=0; for( int i=0; i<c.size(); i=i+1) element in the if(aindex==a.size()){ combined list and c[ i ] = b[bindex]; figure out what the bindex = bindex + 1; element should be. }else if (bindex==b.size()){ c[ i ] = a[aindex]; aindex = aindex + 1; }else if (a[aindex] < b[bindex]){ c[ i ] = a[aindex]; aindex = aindex + 1; } else { c[ i ] = b[bindex]; bindex = bindex + 1; } } Merge Merge c a 2 3 4 7 9 b 1 5 6 8 10 template<class T> void merge( const T & a, const T & b, T & c){ c.resize(a.size() + b.size() ); if we have int aindex=0, bindex=0; exhausted the a for( int i=0; i<c.size(); i=i+1) list then take the if(aindex==a.size()){ next from the b list c[ i ] = b[bindex]; and increment bindex = bindex + 1; bindex }else if (bindex==b.size()){ c[ i ] = a[aindex]; aindex = aindex + 1; }else if (a[aindex] < b[bindex]){ c[ i ] = a[aindex]; aindex = aindex + 1; } else { c[ i ] = b[bindex]; bindex = bindex + 1; } } Merge Merge c a 2 3 4 7 9 b 1 5 6 8 10 template<class T> void merge( const T & a, const T & b, T & c){ c.resize(a.size() + b.size() ); if we have int aindex=0, bindex=0; exhausted the b for( int i=0; i<c.size(); i=i+1) list then take the if(aindex==a.size()){ next from the a list c[ i ] = b[bindex]; and increment bindex = bindex + 1; aindex }else if (bindex==b.size()){ c[ i ] = a[aindex]; aindex = aindex + 1; }else if (a[aindex] < b[bindex]){ c[ i ] = a[aindex]; aindex = aindex + 1; } else { c[ i ] = b[bindex]; bindex = bindex + 1; } } Merge Merge c a 2 3 4 7 9 b 1 5 6 8 10 template<class T> void merge( const T & a, const T & b, T & c){ c.resize(a.size() + b.size() ); otherwise take int aindex=0, bindex=0; from the list with for( int i=0; i<c.size(); i=i+1) the lowest if(aindex==a.size()){ remaining value c[ i ] = b[bindex]; and increment bindex = bindex + 1; its index }else if (bindex==b.size()){ c[ i ] = a[aindex]; aindex = aindex + 1; }else if (a[aindex] < b[bindex]){ c[ i ] = a[aindex]; aindex = aindex + 1; } else { c[ i ] = b[bindex]; bindex = bindex + 1; } } Merge Merge c 1 a 2 3 4 7 9 b 1 5 6 8 10 template<class T> void merge( const T & a, const T & b, T & c){ c.resize(a.size() + b.size() ); int aindex=0, bindex=0; for( int i=0; i<c.size(); i=i+1) if(aindex==a.size()){ c[ i ] = b[bindex]; bindex = bindex + 1; }else if (bindex==b.size()){ c[ i ] = a[aindex]; aindex = aindex + 1; }else if (a[aindex] < b[bindex]){ c[ i ] = a[aindex]; aindex = aindex + 1; } else { c[ i ] = b[bindex]; bindex = bindex + 1; } } Merge Merge c 1 2 a 2 3 4 7 9 b 1 5 6 8 10 template<class T> void merge( const T & a, const T & b, T & c){ c.resize(a.size() + b.size() ); int aindex=0, bindex=0; for( int i=0; i<c.size(); i=i+1) if(aindex==a.size()){ c[ i ] = b[bindex]; bindex = bindex + 1; }else if (bindex==b.size()){ c[ i ] = a[aindex]; aindex = aindex + 1; }else if (a[aindex] < b[bindex]){ c[ i ] = a[aindex]; aindex = aindex + 1; } else { c[ i ] = b[bindex]; bindex = bindex + 1; } } Merge Merge c 1 2 3 a 2 3 4 7 9 b 1 5 6 8 10 template<class T> void merge( const T & a, const T & b, T & c){ c.resize(a.size() + b.size() ); int aindex=0, bindex=0; for( int i=0; i<c.size(); i=i+1) if(aindex==a.size()){ c[ i ] = b[bindex]; bindex = bindex + 1; }else if (bindex==b.size()){ c[ i ] = a[aindex]; aindex = aindex + 1; }else if (a[aindex] < b[bindex]){ c[ i ] = a[aindex]; aindex = aindex + 1; } else { c[ i ] = b[bindex]; bindex = bindex + 1; } } Merge Merge c 1 2 3 4 a 2 3 4 7 9 b 1 5 6 8 10 template<class T> void merge( const T & a, const T & b, T & c){ c.resize(a.size() + b.size() ); int aindex=0, bindex=0; for( int i=0; i<c.size(); i=i+1) if(aindex==a.size()){ c[ i ] = b[bindex]; bindex = bindex + 1; }else if (bindex==b.size()){ c[ i ] = a[aindex]; aindex = aindex + 1; }else if (a[aindex] < b[bindex]){ c[ i ] = a[aindex]; aindex = aindex + 1; } else { c[ i ] = b[bindex]; bindex = bindex + 1; } } Merge Merge a 2 3 4 7 9 b 1 5 6 8 10 c 1 2 3 4 5 template<class T> void merge( const T & a, const T & b, T & c){ c.resize(a.size() + b.size() ); int aindex=0, bindex=0; for( int i=0; i<c.size(); i=i+1) if(aindex==a.size()){ c[ i ] = b[bindex]; bindex = bindex + 1; }else if (bindex==b.size()){ c[ i ] = a[aindex]; aindex = aindex + 1; }else if (a[aindex] < b[bindex]){ c[ i ] = a[aindex]; aindex = aindex + 1; } else { c[ i ] = b[bindex]; bindex = bindex + 1; } } Merge Merge a 2 3 4 7 9 b 1 5 6 8 10 c 1 2 3 4 5 6 template<class T> void merge( const T & a, const T & b, T & c){ c.resize(a.size() + b.size() ); int aindex=0, bindex=0; for( int i=0; i<c.size(); i=i+1) if(aindex==a.size()){ c[ i ] = b[bindex]; bindex = bindex + 1; }else if (bindex==b.size()){ c[ i ] = a[aindex]; aindex = aindex + 1; }else if (a[aindex] < b[bindex]){ c[ i ] = a[aindex]; aindex = aindex + 1; } else { c[ i ] = b[bindex]; bindex = bindex + 1; } } Merge Merge a 2 3 4 7 9 b 1 5 6 8 10 c 1 2 3 4 5 6 7 template<class T> void merge( const T & a, const T & b, T & c){ c.resize(a.size() + b.size() ); int aindex=0, bindex=0; for( int i=0; i<c.size(); i=i+1) if(aindex==a.size()){ c[ i ] = b[bindex]; bindex = bindex + 1; }else if (bindex==b.size()){ c[ i ] = a[aindex]; aindex = aindex + 1; }else if (a[aindex] < b[bindex]){ c[ i ] = a[aindex]; aindex = aindex + 1; } else { c[ i ] = b[bindex]; bindex = bindex + 1; } } Merge Merge a 2 3 4 7 9 b 1 5 6 8 10 c 1 2 3 4 5 6 7 8 template<class T> void merge( const T & a, const T & b, T & c){ c.resize(a.size() + b.size() ); int aindex=0, bindex=0; for( int i=0; i<c.size(); i=i+1) if(aindex==a.size()){ c[ i ] = b[bindex]; bindex = bindex + 1; }else if (bindex==b.size()){ c[ i ] = a[aindex]; aindex = aindex + 1; }else if (a[aindex] < b[bindex]){ c[ i ] = a[aindex]; aindex = aindex + 1; } else { c[ i ] = b[bindex]; bindex = bindex + 1; } } Merge Merge a 2 3 4 7 9 b 1 5 6 8 10 c 1 2 3 4 5 6 7 8 9 10 7 We can create a sort in the following way: 9 If there is only 1 element in the list do nothing 2 Otherwise break the list into two nearly even 4 parts a and b 3 Sort each part 8 Merge the two together 10 1 How do we sort each part? 5 Recursively! 6 Merge Sort Merge Sort 7 We can create a sort in the following way: 9 If there is only 1 element in the list do nothing 2 Otherwise break the list into two nearly even 4 parts a and b 3 Sort each part 8 Merge the two together 10 1 How do we sort each part? 5 Recursively! 6 Merge Sort Merge Sort 2 We can create a sort in the following way: 3 If there is only 1 element in the list do nothing 4 Otherwise break the list into two nearly even 7 parts a and b 9 Sort each part 1 Merge the two together 5 6 How do we sort each part? 8 Recursively! 10 Merge Sort Merge Sort 1 We can create a sort in the following way: 2 If there is only 1 element in the list do nothing 3 Otherwise break the list into two nearly even 4 parts a and b 5 Sort each part 6 Merge the two together 7 8 How do we sort each part? 9 Recursively! 10 Merge Sort Merge Sort template <class T> void mergeSort(T & x) { if (x.size() <= 1) return; int i; T a, b; a.resize(x.size()/2); b.resize(x.size() – a.size()); Merge Sort Merge Sort for(i=0; i< a.size(); i++) a[ i ] = x[ i ]; for(i=0; i< b.size(); i++) b[ i ] = x[ a.size()+i ]; mergeSort(a); mergeSort(b); merge(a, b, x); return; } template <class T> void mergeSort(T & x) { if (x.size() <= 1) return; int i; T a, b; a.resize(x.size()/2); b.resize(x.size() – a.size()); Merge Sort Merge Sort if the size of x is 1 or 0 then there is no need to sort for(i=0; i< a.size(); i++) a[ i ] = x[ i ]; for(i=0; i< b.size(); i++) b[ i ] = x[ a.size()+i ]; mergeSort(a); mergeSort(b); merge(a, b, x); return; } template <class T> void mergeSort(T & x) { if (x.size() <= 1) return; int i; T a, b; a.resize(x.size()/2); b.resize(x.size() – a.size()); Merge Sort Merge Sort Otherwise split the list into two sub-lists that are each half or nearly-half the original list for(i=0; i< a.size(); i++) a[ i ] = x[ i ]; for(i=0; i< b.size(); i++) b[ i ] = x[ a.size()+i ]; mergeSort(a); mergeSort(b); merge(a, b, x); return; } template <class T> void mergeSort(T & x) { if (x.size() <= 1) return; int i; T a, b; a.resize(x.size()/2); b.resize(x.size() – a.size()); Merge Sort Merge Sort Copy the original list into the two sublists for(i=0; i< a.size(); i++) a[ i ] = x[ i ]; for(i=0; i< b.size(); i++) b[ i ] = x[ a.size()+i ]; mergeSort(a); mergeSort(b); merge(a, b, x); return; } template <class T> void mergeSort(T & x) { if (x.size() <= 1) return; int i; T a, b; a.resize(x.size()/2); b.resize(x.size() – a.size()); Merge Sort Merge Sort Sort each half for(i=0; i< a.size(); i++) a[ i ] = x[ i ]; for(i=0; i< b.size(); i++) b[ i ] = x[ a.size()+i ]; mergeSort(a); mergeSort(b); merge(a, b, x); return; } template <class T> void mergeSort(T & x) { if (x.size() <= 1) return; int i; T a, b; a.resize(x.size()/2); b.resize(x.size() – a.size()); Merge Sort Merge Sort Merge them for(i=0; i< a.size(); i++) a[ i ] = x[ i ]; for(i=0; i< b.size(); i++) b[ i ] = x[ a.size()+i ]; mergeSort(a); mergeSort(b); merge(a, b, x); return; } Merge Sort Merge Sort 7 9 2 4 3 8 10 1 5 6 Merge Sort Merge Sort 7 9 2 4 3 8 10 1 5 6 split Merge Sort Merge Sort 7 9 2 4 3 8 10 1 5 6 7 9 2 4 3 sort Merge Sort Merge Sort 7 9 2 4 3 8 10 1 5 6 7 9 2 4 3 split Merge Sort Merge Sort 7 9 2 4 3 8 10 1 5 6 7 9 2 4 3 7 9 sort Merge Sort Merge Sort 7 9 2 4 3 8 10 1 5 6 7 9 2 4 3 7 9 split Merge Sort Merge Sort 7 9 2 4 3 8 10 1 5 6 7 9 2 4 3 merge Merge Sort Merge Sort 7 9 2 4 3 8 10 1 5 6 7 9 2 4 3 2 4 3 sort Merge Sort Merge Sort 7 9 2 4 3 8 10 1 5 6 7 9 2 4 3 2 4 3 split Merge Sort Merge Sort 7 9 2 4 3 8 10 1 5 6 7 9 2 4 3 2 4 3 4 3 sort Merge Sort Merge Sort 7 9 2 4 3 8 10 1 5 6 7 9 2 4 3 2 4 3 4 3 split Merge Sort Merge Sort 7 9 2 4 3 8 10 1 5 6 7 9 2 4 3 2 3 4 merge Merge Sort Merge Sort 7 9 2 4 3 8 10 1 5 6 7 9 2 3 4 merge Merge Sort Merge Sort 2 3 4 7 9 8 10 1 5 6 merge Merge Sort Merge Sort 2 3 4 7 9 8 10 1 5 6 sort 8 10 1 5 6 Merge Sort Merge Sort 2 3 4 7 9 8 10 1 5 6 split 8 10 1 5 6 Merge Sort Merge Sort 2 3 4 7 9 8 10 1 5 6 sort 8 10 1 5 6 8 10 Merge Sort Merge Sort 2 3 4 7 9 8 10 1 5 6 split 8 10 1 5 6 8 10 Merge Sort Merge Sort 2 3 4 7 9 8 10 1 5 6 merge 8 10 1 5 6 Merge Sort Merge Sort 2 3 4 7 9 8 10 1 5 6 sort 8 10 1 5 6 1 5 6 Merge Sort Merge Sort 2 3 4 7 9 8 10 1 5 6 split 8 10 1 5 6 1 5 6 Merge Sort Merge Sort 2 3 4 7 9 8 10 1 5 6 sort 8 10 1 5 6 1 5 6 5 6 Merge Sort Merge Sort 2 3 4 7 9 8 10 1 5 6 split 8 10 1 5 6 1 5 6 5 6 Merge Sort Merge Sort 2 3 4 7 9 8 10 1 5 6 merge 8 10 1 5 6 1 5 6 Merge Sort Merge Sort 2 3 4 7 9 8 10 1 5 6 merge 8 10 1 5 6 Merge Sort Merge Sort 2 3 4 7 9 1 5 6 8 10 merge Merge Sort Merge Sort 1 2 3 4 5 6 7 8 9 10 merge 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 int find (const vector <int> & list, Exercise Exercise 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( ); } 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 int find (const vector <int> & list, Exercise Exercise 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); } Next Lecture Next Lecture Matrices and Linear Equations ...
View Full Document

Ask a homework question - tutors are online