1 / 18

CS 240: Data Structures

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:

rkrauss
Download Presentation

CS 240: Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 240: Data Structures Tuesday, July 8th Vector, Algorithms, Recursion

  2. Vector • Vector is an STL provided sequential container. • It provides us with similar abilities as does our templated mycontainer (lab 4).

  3. 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)

  4. Vector • We can access Vector data as follows: • front() //gets first element • back() //gets last element • operator [unsigned int] //gets element at specified location.

  5. 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);

  6. 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!

  7. Time efficiency • The time an algorithm takes has many variables: • Size of data set • Processing speed • Compiler optimizations, effective coding

  8. 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?

  9. 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

  10. 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

  11. Big Oh • Let us consider a couple of algorithms and determine their complexity. • I have no examples, you’d better think of one.

  12. 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.

  13. 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

  14. 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

  15. Recursion The Biggest! • Base Case: • Termination Case: • Continuation Case: • Easier to evaluate for Big Oh! Too small.

  16. Cheerios are an O with big flavor. • Overstock.com is the “O”. • So, why do we care about the Big O?

  17. 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.

  18. Sorting time! • Insertion Sort! • Two ways to do this… yes… two! • Bubble Sort!

More Related