Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 240: Data Structures Thursday, July 12 th Vector, Algorithms, Recursion.

Similar presentations


Presentation on theme: "CS 240: Data Structures Thursday, July 12 th Vector, Algorithms, Recursion."— Presentation transcript:

1 CS 240: Data Structures Thursday, July 12 th Vector, Algorithms, Recursion

2 Uh oh… The second test is next Thursday. The second test is next Thursday. Hopefully, it won’t be as long. Hopefully, it won’t be as long. Linkedlist presentations are on Tuesday. Linkedlist presentations are on Tuesday.

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

4 Vector We declare a vector just like we do a templated mycontainer: We declare a vector just like we do a templated mycontainer: vector testvector; vector testvector; Many methods are built in: Many methods are built in: Constructor, destructor, operator = Constructor, destructor, operator = size(), capacity(), size(), capacity(), clear() //equivalent to mycontainer::empty() clear() //equivalent to mycontainer::empty() push_back(T) //equivalent to mycontainer::insert(T) push_back(T) //equivalent to mycontainer::insert(T) pop_back(T) //equivalent to mycontainer::remove(T) pop_back(T) //equivalent to mycontainer::remove(T)

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

6 Vector Instead of currentvalue, Vector uses iterators: Instead of currentvalue, Vector uses iterators: vector ::iterator myiterator; //T must match the vector you want to use this iterator with. vector ::iterator myiterator; //T must match the vector you want to use this iterator with. myiterator = testvector.begin(); myiterator = testvector.begin(); myiterator = testvector.end(); myiterator = testvector.end(); myiterator++;//equivalent to mycontainer::next() myiterator++;//equivalent to mycontainer::next() myiterator--;//equivalent to mycontainer::previous() myiterator--;//equivalent to mycontainer::previous() *myiterator;//equivalent to mycontainer::current() *myiterator;//equivalent to mycontainer::current() testvector.erase(myiterator); //equivalent to mycontainer::removeHere(); testvector.erase(myiterator); //equivalent to mycontainer::removeHere(); testvector.insert(myiterator, T); //equivalent to mycontainer::insertHere(T); testvector.insert(myiterator, T); //equivalent to mycontainer::insertHere(T);

7 Algorithm Efficiency What determines if an algorithm is efficient? What determines if an algorithm is efficient? How much space does it take up? How much space does it take up? How long does it take? How long does it take? We usually worry about time when we discuss efficiency – however, space issues are also important! We usually worry about time when we discuss efficiency – however, space issues are also important!

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

9 Time Evaluation We could count how many instructions are executed. 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. Let T(n) represent the time it takes for an algorithm to handle a data size of size n. How long does insert() take? How long does insert() take?

10 Time Evaluation What about taking an average? What about taking an average? How does this vary based on SIZE? How does this vary based on SIZE? SIZE has a direct effect on the performance of this algorithm! 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;

11 Time Evaluation We refer to this as an “order of magnitude” -> Big Oh, or O() 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. In this case, the algorithm is O(N), where N is the input size. Math: Math: We say that T(N) has an order of magnitude of O(f(X)) where, 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. 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 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;

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

13 Recursion Recursion. Recursion. Recursion is a form of loop written in a different style. Recursion is a form of loop written in a different style. Generally, if we have a loop it can become a recursive function. Generally, if we have a loop it can become a recursive function.

14 Recursion Translation 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); }

15 Recursion: Break Down T & search(T searchval, Node * searchat) { if(searchat == NULL) { return NULL; }if(searchat->data==searchval){ return &(searchat->data); } return search(searchval,searchat->next); } Base Case Terminating Case Continuation Case

16 Recursion Base Case: Base Case: Termination Case: Termination Case: Continuation Case: Continuation Case: Minor difference in usage Minor difference in usage Easier to evaluate for Big Oh! Easier to evaluate for Big Oh! Too small.

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

18

19

20 Big Oh It lets us determine how effective one algorithm is compared to another. It lets us determine how effective one algorithm is compared to another. However, it isn’t a perfect answer. But it is pretty good. However, it isn’t a perfect answer. But it is pretty good.

21 Sorting time! Insertion Sort! Insertion Sort! Two ways to do this… yes… two! Two ways to do this… yes… two! Bubble Sort! Bubble Sort!


Download ppt "CS 240: Data Structures Thursday, July 12 th Vector, Algorithms, Recursion."

Similar presentations


Ads by Google