180 likes | 203 Views
CS 240: Data Structures. Tuesday, July 8 th Vector, Algorithms, Recursion. Vector. Vector is an STL provided sequential container. It provides us with similar abilities as does our templated mycontainer (lab 4). Vector. We declare a vector just like we do a templated mycontainer:
E N D
CS 240: Data Structures Tuesday, July 8th Vector, Algorithms, Recursion
Vector • Vector is an STL provided sequential container. • It provides us with similar abilities as does our templated mycontainer (lab 4).
Vector • We declare a vector just like we do a templated mycontainer: • vector<T> testvector; • Many methods are built in: • Constructor, destructor, operator = • size(), capacity(), • clear() //equivalent to mycontainer::empty() • push_back(T) //equivalent to mycontainer::insert(T) • pop_back(T) //equivalent to mycontainer::remove(T)
Vector • We can access Vector data as follows: • front() //gets first element • back() //gets last element • operator [unsigned int] //gets element at specified location.
Vector • Instead of currentvalue, Vector uses iterators: • vector<T>::iterator myiterator; //T must match the vector you want to use this iterator with. • myiterator = testvector.begin(); • myiterator = testvector.end(); • myiterator++; //equivalent to mycontainer::next() • myiterator--; //equivalent to mycontainer::previous() • *myiterator; //equivalent to mycontainer::current() • testvector.erase(myiterator); //equivalent to mycontainer::removeHere(); • testvector.insert(myiterator, T); //equivalent to mycontainer::insertHere(T);
Algorithm Efficiency • What determines if an algorithm is efficient? • How much space does it take up? • How long does it take? • We usually worry about time when we discuss efficiency – however, space issues are also important!
Time efficiency • The time an algorithm takes has many variables: • Size of data set • Processing speed • Compiler optimizations, effective coding
Time Evaluation • We could count how many instructions are executed. • Let T(n) represent the time it takes for an algorithm to handle a data size of size n. • How long does insert() take?
What about taking an average? How does this vary based on SIZE? SIZE has a direct effect on the performance of this algorithm! //float array[SIZE] is filled with data float sum = 0; for(int i=0;i<SIZE;i++) { sum += array[i]; } float average = sum/SIZE; Time Evaluation
We refer to this as an “order of magnitude” -> Big Oh, or O() In this case, the algorithm is O(N), where N is the input size. Math: We say that T(N) has an order of magnitude of O(f(X)) where, T(N) <= Cf(X), for some int constant C, a sufficiently large N and f(X) in terms of N and minimized. f(X) = N, C >= 2 //float array[SIZE] is filled with data float sum = 0; for(int i=0;i<SIZE;i++) { sum += array[i]; } float average = sum/SIZE; Time Evaluation
Big Oh • Let us consider a couple of algorithms and determine their complexity. • I have no examples, you’d better think of one.
Recursion • Recursion. • Recursion is a form of loop written in a different style. • Generally, if we have a loop it can become a recursive function.
T & search(T searchval) { Node * i = first; for(i;i!=NULL;i=i->next) { if(i->data==searchval) { return &(i->data); } } return NULL; } T & search(T searchval, Node * searchat) { if(searchat == NULL) { return NULL; } if(searchat->data==searchval) { return &(searchat->data); } return search(searchval,searchat->next); } Recursion Translation
T & search(T searchval, Node * searchat) { if(searchat == NULL) { return NULL; } if(searchat->data==searchval) { return &(searchat->data); } return search(searchval,searchat->next); } Recursion: Break Down Base Case Terminating Case Continuation Case
Recursion The Biggest! • Base Case: • Termination Case: • Continuation Case: • Easier to evaluate for Big Oh! Too small.
Cheerios are an O with big flavor. • Overstock.com is the “O”. • So, why do we care about the Big O?
Big Oh • It lets us determine how effective one algorithm is compared to another. • However, it isn’t a perfect answer. But it is pretty good.
Sorting time! • Insertion Sort! • Two ways to do this… yes… two! • Bubble Sort!