diff --git a/thu_dsa/chp2/Vector.h b/thu_dsa/chp2/Vector.h index 4cc3550..d6e017f 100644 --- a/thu_dsa/chp2/Vector.h +++ b/thu_dsa/chp2/Vector.h @@ -62,10 +62,10 @@ public: void insert(int pos, T const &elem); T pop_back(void) { return _elem[--_size]; } T pop(int index); - T pop(int lo, int hi);//return the last element popped - int remove(T const &elem);//remove first element matched, return index, -1 if not found - int unique(void); //return the number of elements deleted - int uniquify(void);//unique method for sorted vector + T pop(int lo, int hi); //return the last element popped + int remove(T const &elem); //remove first element matched, return index, -1 if not found + int unique(void); //return the number of elements deleted + int uniquify(void); //unique method for sorted vector void map(void(*visit)(T&)); template void map(VST& visit); diff --git a/thu_dsa/chp3/List.h b/thu_dsa/chp3/List.h index 67ad565..40a1cea 100644 --- a/thu_dsa/chp3/List.h +++ b/thu_dsa/chp3/List.h @@ -11,7 +11,8 @@ private: int size; protected:// internal methods - + ListNodePosi(T) selectMax(ListNodePosi(T) p, int n); + void swap(T &one, T &two); public: //constructors @@ -28,6 +29,8 @@ public: ListNodePosi(T) last() const { return tail->prev; } ListNodePosi(T) find(T const &val) const { return find(val, size, tail); } //find val from all elements of the list ListNodePosi(T) find(T const &val, int n, ListNodePosi(T) p) const; //find val from a range of n elements before p + ListNodePosi(T) search(T const &val, int n, ListNodePosi(T) p) const; //find val in a sorted List + ListNodePosi(T) search(T const &val) const { return search(val, size, tail); } //find val universally ina sorted List T& operator[](int rank); int getSize() const { return size; } bool empty() const { return size == 0; } @@ -44,6 +47,13 @@ public: ListNodePosi(T) pop(int rank); ListNodePosi(T) pop(ListNodePosi(T) p); + //sort algorithms + void selection_sort() { selection_sort(head->succ, size); } + void selection_sort(ListNodePosi(T) p, int n); + void insertion_sort() { insertion_sort(head->succ, size); } + void insertion_sort(ListNodePosi(T) p, int n); + + //deduplicate & uniquify int deduplicate(); int uniquify(); @@ -51,6 +61,24 @@ public: /*----------implementations of class List----------*/ +// internal methods +template +void List::swap(T &one, T &two){ + T tmp = one; + one = two; + two = tmp; +} + +template +ListNodePosi(T) List::selectMax(ListNodePosi(T) p, int n){ + ListNodePosi(T) maxPosi = p; + while(--n){ + p = p->succ; + if (maxPosi->val < p->val) maxPosi = p; + } + return maxPosi; +} + //constructors template List::List(){ @@ -116,6 +144,15 @@ ListNodePosi(T) List::find(T const &val, int n, ListNodePosi(T) p) const { return nullptr; } +template +ListNodePosi(T) List::search(T const &val, int n, ListNodePosi(T) p) const{ + for(int ix = 0; ix != n; ++ix){ + p = p->prev; + if (p->val <= val) return p; + } + return p->prev; +} + template T& List::operator[](int rank){ ListNodePosi(T) p = head->succ; @@ -190,6 +227,31 @@ ListNodePosi(T) List::pop(ListNodePosi(T) p){ return p; } +//sort algorithms +template +void List::selection_sort(ListNodePosi(T) p, int n){ + ListNodePosi(T) currMax; + ListNodePosi(T) tail = p; + for (int ix = 0; ix != n; ++ix, tail = tail->succ); + + for (; n != 1; --n) { + currMax = selectMax(p, n); + swap(currMax->val, tail->prev->val); + tail = tail->prev; + } +} + +template +void List::insertion_sort(ListNodePosi(T) p, int n){ + ListNodePosi(T) target; + for(int ix = 1; ix != n; ix++){ + p = p->succ; + target = search(p->val, ix, p); + insert_before(target->succ, p->val); + pop(p); + } +} + //deduplicate & uniquify template int List::deduplicate(){ diff --git a/thu_dsa/chp3/test_list.cpp b/thu_dsa/chp3/test_list.cpp index 6439d3a..a616fab 100644 --- a/thu_dsa/chp3/test_list.cpp +++ b/thu_dsa/chp3/test_list.cpp @@ -11,6 +11,9 @@ void test_pop(); void test_find(); void test_deduplicate(); void test_uniquify(); +void test_search(); +void test_selection_sort(); +void test_insertion_sort(); int main(){ cout << "Running tests......" << endl; @@ -21,6 +24,9 @@ int main(){ test_find(); test_deduplicate(); test_uniquify(); + test_search(); + test_selection_sort(); + test_insertion_sort(); cout << "All tests passed." << endl; system("pause"); @@ -130,13 +136,64 @@ void test_deduplicate(){ void test_uniquify(){ List l1; int i[] = {5,8,12,14,15,19,20,22,29,36}; - for (int ix = 0; ix != 11; ix++) { + for (int ix = 0; ix != 10; ix++) { l1.push_back(i[ix]); l1.push_back(i[ix]); } - assert(l1.getSize() == 22); - assert(l1.uniquify() == 11); - for (int ix = 0; ix != 11; ix++) + assert(l1.getSize() == 20); + assert(l1.uniquify() == 10); + for (int ix = 0; ix != 10; ix++) assert(l1[ix] == i[ix]); } + +void test_search(){ + List l1; + int i[] = { 5,8,12,14,15,19,20,22,29,36 }; + for (int ix = 0; ix != 10; ix++) { + l1.push_back(i[ix]); + } + + assert(l1.search(8)->val == 8); + assert(l1.search(14, 6, l1.last())->val == 14); + assert(l1.search(30, 5, l1.last())->val == 29); + assert(l1.search(1, 5, l1.last())->val == 14); +} + +void test_selection_sort(){ + List l1; + int a[] = { 2,9,8,4,1,0,7,6,3,5 }; + for (int ix = 0; ix != 10; ix++) { + l1.push_back(a[ix]); + } + + l1.selection_sort(l1.first(), 5); + assert(l1[0] == 1); + assert(l1[1] == 2); + assert(l1[2] == 4); + assert(l1[3] == 8); + assert(l1[4] == 9); + + l1.selection_sort(); + for (int ix = 0; ix != 10; ++ix) + assert(l1[ix] == ix); +} + +void test_insertion_sort(){ + List l1; + int a[] = { 2,9,8,4,1,0,7,6,3,5 }; + for (int ix = 0; ix != 10; ix++) { + l1.push_back(a[ix]); + } + + l1.insertion_sort(l1.first(), 5); + assert(l1[0] == 1); + assert(l1[1] == 2); + assert(l1[2] == 4); + assert(l1[3] == 8); + assert(l1[4] == 9); + + l1.insertion_sort(); + for (int ix = 0; ix != 10; ++ix) + assert(l1[ix] == ix); +}