Generic algorithms
Download
1 / 33

Generic Algorithms - PowerPoint PPT Presentation


  • 195 Views
  • Uploaded on

Generic Algorithms. Andy Wang Data Structures, Algorithms, and Generic Programming. Generic Copy. template <class I, class J> void g_copy(I src_begin, I src_end, J dest_begin) { while (src_begin != src_end) { *dest_begin++ = *src_begin++; } } g_copy(L.Begin(), L.End(), V.Begin());

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Generic Algorithms' - Mercy


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Generic algorithms l.jpg

Generic Algorithms

Andy Wang

Data Structures, Algorithms, and Generic Programming


Generic copy l.jpg
Generic Copy

template <class I, class J>

void g_copy(I src_begin, I src_end, J dest_begin) {

while (src_begin != src_end) {

*dest_begin++ = *src_begin++;

}

}

g_copy(L.Begin(), L.End(), V.Begin());

g_copy(A, A + n, L.Begin());


Generic find l.jpg
Generic Find

  • Sequential search

    template <class I, typename T>

    I g_find(I begin, I end, const T& t) {

    for (; begin != end; ++begin) {

    if (t == *begin) {

    return begin;

    }

    }

    return end;

    }

    List_iterator = g_find(L.Begin(), L.End(), t);

    Vector_iterator = g_find(V.Begin(), V.End(), t);

    Array_iterator = g_find(A, A + size, t);


Generic max l.jpg
Generic Max

template <class I>

I g_max_element(I begin, I end) {

I max(begin);

while (++begin != end) {

if (*max < *begin) {

max = begin;

}

}

return max;

}

List_iterator = g_max_element(L.Begin, L.End());

Deque_iterator = g_max_element(D.Begin, D.End());

Array_iterator = g_max_element(A, A + size);


Generic algorithms and predicate objects l.jpg
Generic Algorithms and Predicate Objects

template <class I, class P>

I g_max_element(I begin, I end, const P& p) {

I max(begin);

while (++begin != end) {

if (p(*max, *begin)) {

max = begin;

}

}

return max;

}

TGreaterThan <sometype> gt;

Vector_iterator = g_max_element(V.Begin(), V.End(), gt);

Deque_iterator = g_max_element(D.Begin(), D.End(), gt);

A_iterator = g_max_element(A, A + size, gt);


Generic algorithms and predicate objects 2 l.jpg
Generic Algorithms and Predicate Objects (2)

  • Function classes may be passed to generic algorithm as template parameter

  • Commonly used to pass predicate such as LessThan

  • Also used for other function classes


Generic algorithms and predicate objects 3 l.jpg
Generic Algorithms and Predicate Objects (3)

template <class I, class P>

F g_for_each(I begin, I end, F f) {

while (begin != end) {

f(*begin++);

}

return f;

}

TList<char> L;

makeuppercase muc;

g_for_each(L.Begin(), L.End(), muc);


Generic for each l.jpg
Generic for_each

class smartmakeuppercase {

public:

int count;

smartmakeuppercase() : count(0) { }

void operator() (char& x) {

char y = x;

x = toupper(x);

if (y != x) ++count;

}

};

smartmakeuppercase smuc0, smuc1;

smuc1 = g_for_each(L.Begin(), L.End(), smuc0);

cout << smuc1.count – smuc0.count; // number letters converted


Testing generic algorithms l.jpg
Testing Generic Algorithms

typedef char element_type;

TList<element_type> L;

TVector<element_type> V;

TList<element_type>::Iterator L_iterator;

TVector<element_type>::Iterator V_iterator;

g_copy(L.Begin(), L.End(), V.Begin());


Generic simple sort l.jpg

begin

end

i

k

j

Generic Simple Sort

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 2 l.jpg

begin

end

i

k

j

Generic Simple Sort (2)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 3 l.jpg

begin

end

i

k

j

Generic Simple Sort (3)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 4 l.jpg

begin

end

i

k

j

Generic Simple Sort (4)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 5 l.jpg

begin

end

i

k

j

Generic Simple Sort (5)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 6 l.jpg

begin

end

i

k

j

Generic Simple Sort (6)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 7 l.jpg

begin

end

i

k

j

Generic Simple Sort (7)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 8 l.jpg

begin

end

i

k

j

Generic Simple Sort (8)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 9 l.jpg

begin

end

i

k

j

Generic Simple Sort (9)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 10 l.jpg

begin

end

i

k

j

Generic Simple Sort (10)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 11 l.jpg

begin

end

i

k

j

Generic Simple Sort (11)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 12 l.jpg

begin

end

i

k

j

Generic Simple Sort (12)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 13 l.jpg

begin

end

i

k

j

Generic Simple Sort (13)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 14 l.jpg

begin

end

i

k

j

Generic Simple Sort (14)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 15 l.jpg

begin

end

i

k

j

Generic Simple Sort (15)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 16 l.jpg

begin

end

i

k

j

Generic Simple Sort (16)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 17 l.jpg

begin

end

i

k

j

Generic Simple Sort (17)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 18 l.jpg

begin

end

i

k

j

Generic Simple Sort (18)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 19 l.jpg

begin

end

i

k

j

Generic Simple Sort (19)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 20 l.jpg

begin

end

i

k

j

Generic Simple Sort (20)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 21 l.jpg

begin

end

i

k

j

Generic Simple Sort (21)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) {

k = i;

for (j = i; j != end; ++j) {

if (*j < *k) {

k = j;

}

}

swap(*i, *k);

}

}


Generic simple sort 22 l.jpg
Generic Simple Sort (22)

template <class T>

void g_simple_sort(I begin, I end) {

I i, j, k;

for (i = begin; i != end; ++i) { // for each element

k = i;

// find the smallest element between i and end

for (j = i; j != end; ++j) {

if (*j < *k) {

// store the index of the smallest

// element to k

k = j;

}

}

// swap ith element with k

swap(*i, *k);

}

}


Generic simple sort 23 l.jpg
Generic Simple Sort (23)

TList<int> L;

g_simple_sort(L.Begin(), L.End());

  • Running time: O(n2)


Announcements l.jpg
Announcements

  • For this week, the Th 2-3 office hour is moved to Fri 11-12

  • On Monday (10/20) and Wednesday (10/22), the class will meet in MCH 201

  • Assignment 3 is due next Monday