Generic Algorithms Andy Wang Data Structures Algorithms and
- Slides: 33
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()); g_copy(A, A + n, L. Begin());
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 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 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; } TGreater. Than <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) Function classes may be passed to generic algorithm as template parameter ¢ Commonly used to pass predicate such as Less. Than ¢ Also used for other function classes ¢
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 class smartmakeuppercase { public: int count; smartmakeuppercase() : count(0) { } void operator() (char& x) { char y = x; x = toupper(x); if (y != x) ++count; } }; smartmakeuppercase smuc 0, smuc 1; smuc 1 = g_for_each(L. Begin(), L. End(), smuc 0); cout << smuc 1. count – smuc 0. count; // number letters converted
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 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); } } begin 1 i k j 8 2 7 3 end
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); } } begin 1 i k 8 j 2 7 3 end
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); } } begin 1 i k 8 2 j 7 3 end
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); } } begin 1 i k 8 2 7 j 3 end
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); } } begin 1 i k 8 2 7 3 j end
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); } } begin 1 i k 8 2 7 3 j end
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); } } begin 1 8 i k j 2 7 3 end
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); } } begin 1 8 i k 2 j 7 3 end
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); } } begin 1 8 i 2 k j 7 3 end
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); } } begin 1 8 i 2 k 7 j 3 end
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); } } begin 1 8 i 2 k 7 3 j end
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); } } begin 1 2 i 8 k 7 3 j end
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); } } begin 1 2 8 i kj 7 3 end
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); } } begin 1 2 8 i k 7 j 3 end
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); } } begin 1 2 8 i 7 kj 3 end
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); } } begin 1 2 8 i 7 k 3 j end
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); } } begin 1 2 8 i 7 3 kj end
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); } } begin 1 2 3 i 7 8 kj end
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); } } begin 1 2 3 7 i kj 8 end
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); } } begin 1 2 3 7 i k 8 j end
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); } } begin 1 2 3 7 8 i kj end
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) TList<int> L; g_simple_sort(L. Begin(), L. End()); ¢ Running time: O(n 2)
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 ¢
- Ajit diwan iit bombay
- Cos 423
- Data structures and algorithms tutorial
- Information retrieval data structures and algorithms
- Data structures and algorithms bits pilani
- Ajit diwan iitb
- Data structures and algorithms
- Data structures and algorithms
- Waterloo data structures and algorithms
- Information retrieval data structures and algorithms
- Data structures and algorithms
- Algorithms + data structures = programs
- Andy wang fsu
- Andy wang fsu
- Homologous structures examples
- Generic two level data warehouse architecture
- Macro expansion in system software
- Assembler data structures
- Data structures and abstractions with java
- Adts, data structures, and problem solving with c++
- Data structures and algorithm
- Persistent and ephemeral data structures
- Data stream
- Li you and wang peng
- Btechsmartclass data structures
- Data structures in r
- Oblivious data structures
- Linux kernel map data structure
- Introduction to data structures
- Introduction to data structures
- Esoteric data structures
- Geometric data structures
- Hadoop i/o hadoop comes with a set of
- Advanced data structures in java