Top Banner
CS 240: Data CS 240: Data Structures Structures Thursday, July 12 Thursday, July 12 th th Vector, Algorithms, Vector, Algorithms, Recursion Recursion
21

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

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 240: Data Structures Thursday, July 12 th Vector, Algorithms, Recursion.

CS 240: Data StructuresCS 240: Data Structures

Thursday, July 12Thursday, July 12thth

Vector, Algorithms, RecursionVector, Algorithms, Recursion

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

Uh oh…Uh oh…

The second test is The second test is next Thursday.next Thursday. Hopefully, it won’t be Hopefully, it won’t be

as long.as long.

Linkedlist Linkedlist presentations are on presentations are on Tuesday.Tuesday.

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

VectorVector

Vector is an STL provided sequential Vector is an STL provided sequential container.container.

It provides us with similar abilities as does It provides us with similar abilities as does our templated mycontainer (lab 5).our templated mycontainer (lab 5).

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

VectorVector

We declare a vector just like we do a templated We declare a vector just like we do a templated mycontainer:mycontainer: vector<T> testvector;vector<T> 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)

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

VectorVector

We can access Vector data as follows:We can access Vector data as follows: front() //gets first elementfront() //gets first elementback() //gets last elementback() //gets last elementoperator [unsigned int] //gets element at operator [unsigned int] //gets element at

specified location.specified location.

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

VectorVector Instead of currentvalue, Vector uses iterators:Instead of currentvalue, Vector uses iterators:

vector<T>::iterator myiterator; //T must match the vector<T>::iterator myiterator; //T must match the vector you want to use this iterator with.vector you want to use this iterator with.

myiterator = testvector.begin();myiterator = testvector.begin(); myiterator = testvector.end();myiterator = testvector.end(); myiterator++;myiterator++; //equivalent to mycontainer::next()//equivalent to mycontainer::next() myiterator--;myiterator--; //equivalent to mycontainer::previous()//equivalent to mycontainer::previous() *myiterator;*myiterator; //equivalent to mycontainer::current()//equivalent to mycontainer::current() testvector.erase(myiterator); //equivalent to testvector.erase(myiterator); //equivalent to

mycontainer::removeHere();mycontainer::removeHere(); testvector.insert(myiterator, T); //equivalent to testvector.insert(myiterator, T); //equivalent to

mycontainer::insertHere(T);mycontainer::insertHere(T);

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

Algorithm EfficiencyAlgorithm Efficiency

What determines if an algorithm is What determines if an algorithm is efficient?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 We usually worry about time when we discuss efficiency – however, space discuss efficiency – however, space issues are also important!issues are also important!

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

Time efficiencyTime efficiency

The time an algorithm takes has many The time an algorithm takes has many variables:variables:Size of data setSize of data setProcessing speedProcessing speedCompiler optimizations, effective codingCompiler optimizations, effective coding

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

Time EvaluationTime Evaluation

We could count how many instructions are We could count how many instructions are executed.executed.

Let T(n) represent the time it takes for an Let T(n) represent the time it takes for an algorithm to handle a data size of size n.algorithm to handle a data size of size n.

How long does insert() take?How long does insert() take?

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

Time EvaluationTime Evaluation

What about taking an What about taking an average?average?

How does this vary based How does this vary based on SIZE?on SIZE?

SIZE has a direct effect SIZE has a direct effect on the performance of on the performance of this algorithm!this algorithm!

//float array[SIZE] is filled //float array[SIZE] is filled with datawith data

float sum = 0;float sum = 0;

for(int i=0;i<SIZE;i++)for(int i=0;i<SIZE;i++)

{{

sum += array[i];sum += array[i];

}}

float average = sum/SIZE;float average = sum/SIZE;

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

Time EvaluationTime Evaluation We refer to this as an We refer to this as an

“order of magnitude” -> “order of magnitude” -> Big Oh, or O()Big Oh, or O()

In this case, the algorithm In this case, the algorithm is O(N), where N is the is O(N), where N is the input size.input size.

Math:Math: We say that T(N) has an We say that T(N) has an

order of magnitude of order of magnitude of O(f(X)) where,O(f(X)) where,

T(N) <= Cf(X), for some int T(N) <= Cf(X), for some int constant C, a sufficiently constant C, a sufficiently large N and f(X) in terms of large N and f(X) in terms of N and minimized.N and minimized.

f(X) = N, C >= 2f(X) = N, C >= 2

//float array[SIZE] is filled //float array[SIZE] is filled with datawith data

float sum = 0;float sum = 0;for(int i=0;i<SIZE;i++)for(int i=0;i<SIZE;i++){{

sum += array[i];sum += array[i];}}float average = sum/SIZE;float average = sum/SIZE;

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

Big OhBig Oh

Let us consider a couple of algorithms and Let us consider a couple of algorithms and determine their complexity.determine their complexity.

I have no examples, you’d better think of I have no examples, you’d better think of one.one.

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

RecursionRecursion

Recursion.Recursion. Recursion is a form of Recursion is a form of

loop written in a loop written in a different style.different style.

Generally, if we have Generally, if we have a loop it can become a loop it can become a recursive function.a recursive function.

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

Recursion TranslationRecursion Translation

T & search(T searchval)T & search(T searchval){{

Node * i = first;Node * i = first;for(i;i!=NULL;i=i->next)for(i;i!=NULL;i=i->next){{

if(i->data==searchval)if(i->data==searchval){{

return &(i-return &(i->data);>data);

}}}}return NULL;return NULL;

}}

T & search(T searchval, Node * searchat)T & search(T searchval, Node * searchat){{

if(searchat == NULL)if(searchat == NULL){{

return NULL;return NULL;}}if(searchat->data==searchval)if(searchat->data==searchval){{

return &(searchat->data);return &(searchat->data);}}return search(searchval,searchat-return search(searchval,searchat->next);>next);

}}

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

Recursion: Break DownRecursion: Break Down

T & search(T searchval, Node * searchat)T & search(T searchval, Node * searchat)

{{

if(searchat == NULL)if(searchat == NULL)

{{

return NULL;return NULL;

}}

if(searchat->data==searchval)if(searchat->data==searchval)

{{

return &(searchat->data);return &(searchat->data);

}}

return search(searchval,searchat->next);return search(searchval,searchat->next);

}}

Base Case

Terminating Case

Continuation Case

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

RecursionRecursion

Base Case:Base Case: Termination Case:Termination Case: Continuation Case:Continuation Case: Minor difference in Minor difference in

usageusage Easier to evaluate for Easier to evaluate for

Big Oh!Big Oh!

Too small.

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

Cheerios are an O Cheerios are an O with big flavor.with big flavor.

Overstock.com is the Overstock.com is the “O”.“O”.

So, why do we care So, why do we care about the Big O?about the Big O?

Page 18: CS 240: Data Structures Thursday, July 12 th Vector, Algorithms, Recursion.
Page 19: CS 240: Data Structures Thursday, July 12 th Vector, Algorithms, Recursion.
Page 20: CS 240: Data Structures Thursday, July 12 th Vector, Algorithms, Recursion.

Big OhBig Oh

It lets us determine how effective one It lets us determine how effective one algorithm is compared to another.algorithm is compared to another.

However, it isn’t a perfect answer. But it is However, it isn’t a perfect answer. But it is pretty good.pretty good.

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

Sorting time!Sorting time!

Insertion Sort!Insertion Sort!Two ways to do this… yes… two!Two ways to do this… yes… two!

Bubble Sort!Bubble Sort!