CS2134 Notes

CS2134 Notes - 9/5/07 Data Structures and Algorithm...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 9/5/07 Data Structures and Algorithm Analysis in C++ 2 nd Edition Big-Oh (Order) O(1) - constant O(log n) - logarithmic O(n) linear O(nlog n) O(n 2 ) - quadratic O(n 3 ) cubic Sequential Search O(n) Binary Search O(log n) Selection Sort - O(n 2 ) Merge Sort O(nlog n) 9/10/07 Given sequence A 1 ,,A n of numbers. Find i and j such that A i + + A j is maximal. 1, 2, -4, 1, 2, -1, 4, -2, 1 i=4, j=7 (1, 2, -1, 4) = 5 Brain-dead way of doing it is to go through all possible combinations. Better way is to add and check until sequence gets negative. Then make i = j + 1 and continue from there. O(n). If n = b k , then k = log b n. log b a = logcalogcb Repeated Halving $n -> $1 After i times, amount remaining is n/2 i . n/2 i = $1 log 2 n = i Repeated Doubling $1 -> $n log 2 n = i 9/12/07 To calculated running time: t = cn k template <class Object> void swap(Object& x, Object& y) {} template <class Object> class MyClass {Object data[5];} MyClass<int> x; MyClass<double> y; 9/17/07 const vector<Object>& vector<Object>::operator=(const vector<Object>& rhs) { if(this != &rhs) { delete object; theSize = rhs.theSize; theCapacity = rhs.theCapacity; objects = new Object[theCapacity]; for(size_t i = 0; i < theSize; i++) { objects[i] = rhs.objects[i]; } return *this; } } class student { private: string name; double gpa; public: string get_name(); double get_gpa() bool operator<(student rhs){}; } template <class Comparable> int findmax(const vector<Comparable>& a) { int maxIndex = 0; if(a[maxIndex] < a[i]) } What if you want to compare different parts of student at different parts of the same program? One Way template <class Object, class Comparator> const Object& findMax(const vector<object>& a, Comparator comp) { int maxIndex = 0; for(int i = 1; i < a.size(); i++) { if(comp.isLessThan(a[maxIndex], a[i])) maxIndex = i; } return a[maxIndex]; } class LessThanByGPA { public: bool isLessThan(const Student& lhs, const Student& rhs) const { return lhs.get_GPA() < rhs.get_GPA(); } }; class LessThanByName { public: bool isLessThan(const Student& lhs, const Student& rhs) const { return lhs.get_name() < rhs.get_name(); } }; int main() { vector<student> a; Object maxname = findMax(a, LessThanByName()); Object maxgpa = findMax(a, LessThanByGPA()); } Another Way template <class Object, class Comparator> const Object& findMax(const vector<object>& a, Comparator isLessThan) { int maxIndex = 0; for(int i = 1; i < a.size(); i++) { if(isLessThan(a[maxIndex], a[i])) maxIndex = i; } return a[maxIndex]; } class LessThanByLength { public: bool operator() (const Rectangle& lhs, const Rectangle& rhs) const...
View Full Document

Page1 / 39

CS2134 Notes - 9/5/07 Data Structures and Algorithm...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online