Lectures 11,12 1. Quicksort algorithm 2. Mergesort algorithm 3. Big O notation 4. Estimating computational efficiency of binary search, quicksort and mergesort algorithms 5. Basic Data Structures: Arrays Lists Trees Hash Tables 6. Standard Template Library of C++ Basic components of STL: containers, algorithms and iterators Online documentation & links http://www.sgi.com/tech/stl/ Several examples Taken from: http://msdn.microsoft.com/library/default.asp?url=/library/enus/vclang98/html/sample.asp An example of a container class: List Required Header: <list> reference back(); reference front(); void pop_back(); void pop_front(); void push_back(const T& x); void push_front(const T& x); The member function back returns a reference to the last element of the controlled sequence. The member function front returns a reference to the first element of the controlled sequence. The member function pop_back removes the last element of the controlled sequence. The member function pop_front removes the first element of the controlled sequence. All the above functions require that the controlled sequence be nonempty. The member function push_back inserts an element with value x at the end of the controlled sequence. The member function push_front inserts an element with value x at the beginning of the controlled sequence.
void assign(const_iterator first, const_iterator last); void assign(size_type n, const T& x = T()); iterator erase(iterator it); iterator erase(iterator first, iterator last); bool empty() const; The first member function replaces the sequence controlled by *this with the sequence [first, last). The second member function replaces the sequence controlled by *this with a repetition of n elements of value x. The third member function removes the element of the controlled sequence pointed to by it. The fourth member function removes the elements of the controlled sequence in the range [first, last). Both return an iterator that designates the first element remaining beyond any elements removed, or end() if no such element exists. The last member function returns true for an empty controlled sequence. #include <list> #include <iostream> using namespace std ; typedef list<int> LISTINT; int main() { //Example 1 LISTINT test; test.push_back(1); test.push_front(2); test.push_front(3); // front cout << test.front() << endl; // back cout << test.back() << endl; test.pop_front(); test.pop_back(); // middle cout << test.front() << endl; //Example 2 LISTINT listone; LISTINT listtwo; LISTINT::iterator i;
// Add some data listone.push_front (2); listone.push_front (1); listone.push_back (3); listtwo.push_front(4); listtwo.assign(listone.begin(), listone.end()); // 1 2 3 for (i = listtwo.begin(); i!= listtwo.end(); ++i) cout << *i << " "; cout << endl; listtwo.assign(4, 1); // 1 1 1 1 for (i = listtwo.begin(); i!= listtwo.end(); ++i) cout << *i << " "; cout << endl; listtwo.erase(listtwo.begin()); // 1 1 1 for (i = listtwo.begin(); i!= listtwo.end(); ++i) cout << *i << " "; cout << endl; listtwo.erase(listtwo.begin(), listtwo.end()); if (listtwo.empty()) cout << "All gone\n"; return 0; An example of an algorithm: Merge Required Header: <algorithm> OutputIterator merge( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result ) The merge algorithm merges two sorted sequences: [first1..last1) and [first2..last2) into a single sorted sequence starting at result. This version assumes that the ranges
[first1..last1) and [first2..last2) are sorted using operator<. If both ranges contain equal values, the value from the first range will be stored first. The result of merging overlapping ranges is undefined. #include <iostream> #include <algorithm> #include <vector> #include <list> #include <deque> using namespace std ; int main() { const int MAX_ELEMENTS = 8 ; // Define a template class vector of int typedef vector<int> IntVector ; //Define an iterator for template class vector of ints typedef IntVector::iterator IntVectorIt ; IntVector NumbersVector(MAX_ELEMENTS) ; IntVectorIt startv, endv, itv ; // Define a template class list of int typedef list<int> IntList ; //Define an iterator for template class list of ints typedef IntList::iterator IntListIt ; IntList NumbersList ; IntListIt first, last, itl ; // Define a template class deque of int typedef deque<int> IntDeque ; //Define an iterator for template class deque of ints typedef IntDeque::iterator IntDequeIt ; IntDeque NumbersDeque(2 * MAX_ELEMENTS) ; IntDequeIt itd ;
// Initialize vector NumbersVector NumbersVector[0] = 4 ; NumbersVector[1] = 10; NumbersVector[2] = 70 ; NumbersVector[3] = 10 ; NumbersVector[4] = 30 ; NumbersVector[5] = 69 ; NumbersVector[6] = 96 ; NumbersVector[7] = 100; startv = NumbersVector.begin() ; // location of first // element of NumbersVector endv = NumbersVector.end() ; // one past the location // last element of NumbersVector // sort NumbersVector, merge requires the sequences // to be sorted sort(startv, endv) ; // print content of NumbersVector cout << "NumbersVector { " ; for(itv = startv; itv!= endv; itv++) cout << *itv << " " ; cout << " \n" << endl ; // Initialize vector NumbersList for(int i = 0; i < MAX_ELEMENTS; i++) NumbersList.push_back(i) ; first = NumbersList.begin() ; // location of first // element of NumbersList last = NumbersList.end() ; // one past the location // last element of NumbersList // print content of NumbersList cout << "NumbersList { " ; for(itl = first; itl!= last; itl++) cout << *itl << " " ; cout << " \n" << endl ; // merge the elements of NumbersVector // and NumbersList and place the // results in NumbersDeque merge(startv, endv, first, last, NumbersDeque.begin()) ;
cout << "After calling merge\n" << endl ; // print content of NumbersDeque cout << "NumbersDeque { " ; for(itd = NumbersDeque.begin();itd!= NumbersDeque.end(); itd++) cout << *itd << " " ; cout << " \n" << endl ; return 0; 7. First encounter with Java Documentation and downloads http://java.sun.com/ http://java.sun.com/docs/books/tutorial/getstarted/cupojava/unix.html /** * The HelloWorldApp class implements an application that * simply displays "Hello World!" to the standard output. */ class HelloWorldApp { public static void main(string[] args) { System.out.println("Hello World!"); // Display "Hello World! docs/books/tutorial/essential/attributes/cmdlineargs.html public class Echo { public static void main (String[] args) { for (int i = 0; i < args.length; i++) System.out.println(args[i]); http://java.sun.com/applets/