Week 1 1 Programming Style 2 Standard Template Library 3 Merge Sort Algorithm Programming Style C 1 Names of functions classes structures variables and constants 2 Expressions and statements 3 Comments Basic components of STL 1 Containers 2 Algorithms 3 Iterators Online documentation links http www sgi com tech stl Merge algorithm OutputIterator merge InputIterator1 first1 InputIterator1 last1 InputIterator2 first2 InputIterator2 last2 OutputIterator result Merge combines two sorted ranges first1 last1 and first2 last2 into a single sorted range That is it copies elements from first1 last1 and first2 last2 into result result last1 first1 last2 first2 such that the resulting range is in ascending order The return value is result last1 first1 last2 first2 Include statements include vector include algorithm Type definitions typedef vector int IntVector typedef IntVector iterator IntVectorIt Allocate a vector of size n Define an iterator and its initial and terminal values IntVector Vector1 n IntVectorIt start1 end1 it1 start1 Vector1 begin end1 Vector1 end Enter n consecutive even numbers into vector 1 int i 0 for it1 start1 it1 end1 it1 it1 2 i i Perform merging of vectors 1 and 2 into vector 3 merge start1 end1 start2 end2 start3 Sort Algorithm void sort RandomAccessIterator first RandomAccessIterator 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 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
View Full Document
Unlocking...