Lectures 19, 20, 21 1. STL library examples of applications Explanations: The member function pop_back removes the last element of the controlled sequence. The member function pop_front removes the first element of the controlled sequence. Both the above functions require that the controlled sequence is non-empty. The member function push_back inserts an element at the end of the controlled sequence. The member function push_front inserts an element at the beginning of the controlled sequence. OutputIterator merge( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result ) The merge algorithm merges two sorted sequences: [first1..last1) and [first2..last2) into a single sorted sequence starting at result. It assumes that the ranges [first1..last1) and [first2..last2) are sorted using operator<. void sort(iterator first, Iterator last); Sort sorts the elements in [first, last) into ascending order, meaning that if i and j are any two valid iterators in [first, last) such that i precedes j, then *j is not less than *i. #include <iostream> #include <algorithm> #include <vector> #include <list> #include <deque> using namespace std ; int main() { const int MAX_ELEMENTS = 8 ; // Define a template class vector of int typedef vector<int> IntVector ; //Define an iterator for template class vector of ints typedef IntVector::iterator IntVectorIt ; IntVector NumbersVector(MAX_ELEMENTS) ; IntVectorIt startv, endv, itv ; // Define a template class list of int typedef list<int> IntList ;
//Define an iterator for template class list of ints typedef IntList::iterator IntListIt ; IntList NumbersList ; IntListIt first, last, itl ; // Define a template class deque of int typedef deque<int> IntDeque ; //Define an iterator for template class deque of ints typedef IntDeque::iterator IntDequeIt ; IntDeque NumbersDeque(2 * MAX_ELEMENTS) ; IntDequeIt itd ; // Initialize vector NumbersVector NumbersVector[0] = 4 ; NumbersVector[1] = 10; NumbersVector[2] = 70 ; NumbersVector[3] = 10 ; NumbersVector[4] = 30 ; NumbersVector[5] = 69 ; NumbersVector[6] = 96 ; NumbersVector[7] = 100; startv = NumbersVector.begin() ; // location of first // element of NumbersVector endv = NumbersVector.end() ; // one past the location // last element of NumbersVector // sort NumbersVector, merge requires the sequences // to be sorted sort(startv, endv) ; // print content of NumbersVector cout << "NumbersVector { " ; for(itv = startv; itv!= endv; itv++) cout << *itv << " " ; cout << " }\n" << endl ;
// Initialize vector NumbersList for(int i = 0; i < MAX_ELEMENTS; i++) NumbersList.push_back(i) ; first = NumbersList.begin() ; // location of first // element of NumbersList last = NumbersList.end() ; // one past the location // last element of NumbersList // print content of NumbersList cout << "NumbersList { " ; for(itl = first; itl!= last; itl++) cout << *itl << " " ; cout << " }\n" << endl ; // merge the elements of NumbersVector // and NumbersList and place the // results in NumbersDeque merge(startv, endv, first, last, NumbersDeque.begin()) ; cout << "After calling merge\n" << endl ; // print content of NumbersDeque cout << "NumbersDeque { " ; for(itd = NumbersDeque.begin();itd!= NumbersDeque.end(); itd++) cout << *itd << " " ; cout << " }\n" << endl ; } return 0; Output: NumbersVector { 4 10 10 30 69 70 96 100 } NumbersList { 0 1 2 3 4 5 6 7 } After calling merge NumbersDeque { 0 1 2 3 4 4 5 6 7 10 10 30 69 70 96 100 } 2. Timing - review #include <ctime> clock_t start=clock(); CODE TO BE TIMED clock_t stop=clock(); cout << (stop-start) /CLOCKS_PER_SEC << endl;
3. Generating random numbers #include <cstdlib> rand() 4. An example of timing procedure (timing STL sort algorithm on random arrays) #include <ctime> #include <cstdlib> #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { typedef vector<int> IntVector; typedef IntVector::iterator IntVectorIt; size_t n; //Seed the random-number generator with current time so that //the numbers will be different every time we run. srand((unsigned)time(null)); cout << "Enter the size of the random array to be sorted" << endl; cin >> n; cout << endl; IntVector NumbersVector(n); IntVectorIt startv,endv,itv; startv = NumbersVector.begin(); endv = NumbersVector.end(); //Enter random numbers into NumbersVector for(itv=startv;itv!=endv;itv++) *itv=rand()%n; //Start timing sorting procedure clock_t start=clock(); sort(startv,endv); //Stop timing sorting procedure clock_t stop=clock(); //Print out elapsed time in miliseconds cout << "The elapsed time: "; cout << 1000*(stop-start) /CLOCKS_PER_SEC << endl;
} return 0; 5. Merge Sort algorithm void mergesort(int a[], int s, int r) { if(r<=s) return; int m=(r+s)/2; mergesort(a, s, m); mergesort(a, m+1, r); merge(a, s, m, r); } Merge operation is discussed on page 613 of the textbook (the indexing is different than ours). Example: s=0, r=11, a=[8,7,6,5,2,3,4,11,9,10,12,1] The tree of recursive calls: The original data: 8 7 6 5 2 3 4 11 9 10 12 1 After merging on level 3: 7 8 6 2 5 3 4 11 9 10 12 1 After merging on level 2: 6 7 8 2 3 5 4 9 11 1 10 12 After merging on level 1: 2 3 5 6 7 8 1 4 9 10 11 12 After merging on level 0: 1 2 3 4 5 6 7 8 9 10 11 12
6. Analysis of Merge Sort Every execution of Merge Sort algorithm requires the number of operations of order nlog 2 n (n is the size of the array).