1 / 33

# generic algorithms - PowerPoint PPT Presentation

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());

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

## 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

Andy Wang

Data Structures, Algorithms, and Generic Programming

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());

• 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);

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

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

• 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

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

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

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());

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

}

}

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

}

}

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

}

}

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

}

}

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

}

}

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

}

}

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

}

}

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

}

}

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

}

}

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

}

}

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

}

}

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

}

}

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

}

}

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

}

}

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

}

}

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

}

}

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

}

}

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

}

}

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

}

}

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

}

}

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

}

}

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

}

}

TList<int> L;

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

• Running time: O(n2)

• 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