From 47d0086d38a666c645d350db39b8e7547d2939c5 Mon Sep 17 00:00:00 2001 From: Shine wOng <1551885@tongji.edu.cn> Date: Mon, 13 May 2019 14:05:00 +0800 Subject: [PATCH] add Fib class, testFib.cpp, fib_search and bubbleSort --- thu_dsa/chp2/Fib.cpp | 33 ++++++++++++++++++ thu_dsa/chp2/Fib.h | 18 ++++++++++ thu_dsa/chp2/Vector.h | 67 ++++++++++++++++++++++++++++++++++--- thu_dsa/chp2/testFib.cpp | 36 ++++++++++++++++++++ thu_dsa/chp2/testVector.cpp | 39 +++++++++++++++++++++ 5 files changed, 189 insertions(+), 4 deletions(-) create mode 100644 thu_dsa/chp2/Fib.cpp create mode 100644 thu_dsa/chp2/Fib.h create mode 100644 thu_dsa/chp2/testFib.cpp diff --git a/thu_dsa/chp2/Fib.cpp b/thu_dsa/chp2/Fib.cpp new file mode 100644 index 0000000..096f719 --- /dev/null +++ b/thu_dsa/chp2/Fib.cpp @@ -0,0 +1,33 @@ +#include "Fib.h" +#include +using std::cout; +using std::endl; + +//constructor +Fib::Fib(int n) { + elem = Vector(); + int prev = 0, current = 1, tmp; + + elem.push_back(prev); + while (current < n) { + elem.push_back(current); + + tmp = prev; + prev = current; + current = tmp + current; + } +} + +//external interfaces +void Fib::print() { + for (int ix = 0; ix != elem.getSize(); ++ix) + cout << elem[ix] << endl; +} + +int Fib::get() { + return elem[elem.getSize() - 1]; +} + +void Fib::prev() { + elem.pop_back(); +} diff --git a/thu_dsa/chp2/Fib.h b/thu_dsa/chp2/Fib.h new file mode 100644 index 0000000..ca3b372 --- /dev/null +++ b/thu_dsa/chp2/Fib.h @@ -0,0 +1,18 @@ +#ifndef FIB_H_ +#define FIB_H_ + +#include "Vector.h" + +class Fib{ +protected: + Vector elem; +public: + //constructor + Fib(int n); + + void print(); + int get(); + void prev(); +}; + +#endif diff --git a/thu_dsa/chp2/Vector.h b/thu_dsa/chp2/Vector.h index 11f2f7b..183d244 100644 --- a/thu_dsa/chp2/Vector.h +++ b/thu_dsa/chp2/Vector.h @@ -1,6 +1,7 @@ #ifndef VECTOR_H_ #define VECTOR_H_ +#include "Fib.h" #include #define DEFAULT_CAPACITY 3 @@ -21,6 +22,9 @@ protected: void copyfrom(T* const A, int lo, int hi); void copyfrom(Vector const &V, int lo, int hi); void expand(); + void swap(T &one, T &two); + void merge(int lo, int mid, int hi); + int bubble(int lo, int hi); //void shrink(); public: @@ -45,6 +49,8 @@ public: int interpolation_search(T const &elem, int lo, int hi); int getSize() { return _size; } int getCapacity() { return _capacity; } + int unordered() { return unordered(0, _size); } + int unordered(int lo, int hi); bool empty(){ return _size == 0;} //writable interfaces @@ -54,7 +60,7 @@ public: Vector& operator=(Vector const &V); void push_back(T const &elem); void insert(int pos, T const &elem); - T pop_back(void) { return _elem[--_size]; } + 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 @@ -62,12 +68,18 @@ public: int uniquify(void);//unique method for sorted vector void map(void(*visit)(T&)); template void map(VST& visit); + + //sorting algorithms + void bubbleSort() { bubbleSort(0, _size); } + void bubbleSort(int lo, int hi); + void mergeSort() { mergeSort(0, _size); } + void mergeSort(int lo, int hi); }; //protected methods template void Vector::copyfrom(T* const A, int lo, int hi) { - _capacity = (hi - lo) << 1; + _capacity = MAX((hi - lo) << 1, DEFAULT_CAPACITY); _size = 0; _elem = new T[_capacity]; while (lo != hi) _elem[_size++] = A[lo++]; @@ -83,8 +95,28 @@ void Vector::expand(){ if (_size < _capacity) return; //else double capacity T* tmp = _elem; - delete[]_elem; copyfrom(tmp, 0, _size); + delete[]tmp; +} + +template +void Vector::swap(T &one, T &two){ + T temp; + temp = one; + one = two; + two = temp; +} + +template +int Vector::bubble(int lo, int hi){ + int lastSwap = 0; + while(++lo != hi){ + if(_elem[lo] < _elem[lo - 1]){ + swap(_elem[lo], _elem[lo - 1]); + lastSwap = lo; + } + } + return lastSwap; } //constructors @@ -133,6 +165,19 @@ int Vector::search(T const &elem, int lo, int hi){ } return lo - 1; } +template +int Vector:: fib_search(T const &elem, int lo, int hi){ + Fib fib(hi - lo); + int mid; + while(lo < hi){ + while (hi - lo <= fib.get()) fib.prev(); + mid = fib.get() + lo; + if (elem < _elem[mid]) hi = mid; + else if (_elem[mid] < elem) lo = mid + 1; + else return mid; + } + return lo - 1; +} template int Vector::binary_search(T const &elem, int lo, int hi){ @@ -144,6 +189,14 @@ int Vector::binary_search(T const &elem, int lo, int hi){ return lo - 1; } +template +int Vector::unordered(int lo, int hi){ + int reversed = 0; + while(++lo != hi){ + if (_elem[lo] < _elem[lo - 1]) ++reversed; + } + return reversed; +} // writable interfaces template @@ -218,7 +271,7 @@ int Vector::uniquify(){ } template -void Vector::map(void(*visit)(T&)){ +void Vector::map(void(*visit)(T&)) { for (int ix = 0; ix != _size; ++ix) visit(_elem[ix]); } @@ -227,4 +280,10 @@ void Vector::map(VST& visit) { for (int ix = 0; ix != _size; ++ix) visit(_elem[ix]); } +//sorting algorithms +template +void Vector::bubbleSort(int lo, int hi){ + while (lo < (hi = bubble(lo, hi))); +} + #endif diff --git a/thu_dsa/chp2/testFib.cpp b/thu_dsa/chp2/testFib.cpp new file mode 100644 index 0000000..9491bdb --- /dev/null +++ b/thu_dsa/chp2/testFib.cpp @@ -0,0 +1,36 @@ +#include "Fib.h" +#include +#include + +using std::cout; +using std::endl; + +void test_fib_constructor(); +void test_get_and_prev(); + +int test_fib_main(){ + test_fib_constructor(); + test_get_and_prev(); + + system("pause"); + return 0; +} + +void test_fib_constructor(){ + cout << "run test_constructor..."; + Fib fib(100); + + cout << "test passed." << endl; +} + +void test_get_and_prev(){ + cout << "run test_get_and_prev..."; + Fib fib(100); + assert(fib.get() == 89); + fib.prev(); + assert(fib.get() == 55); + fib.prev(); + assert(fib.get() == 34); + + cout << "test passed." << endl; +} diff --git a/thu_dsa/chp2/testVector.cpp b/thu_dsa/chp2/testVector.cpp index 1cef1f0..21a9385 100644 --- a/thu_dsa/chp2/testVector.cpp +++ b/thu_dsa/chp2/testVector.cpp @@ -1,7 +1,12 @@ +#include "Fib.h" #include "Vector.h" +#include #include #include +#define NUMOFLOOPS 1000 +#define ELEMENT_NUM 10000 + using std::cout; using std::endl; @@ -13,6 +18,7 @@ void test_find(); void test_unique(); void test_search(); void test_map(); +void test_bubbleSort(); int main(){ test_constructor(); @@ -22,6 +28,7 @@ int main(){ test_find(); test_unique(); test_search(); + test_bubbleSort(); test_map(); system("pause"); @@ -163,6 +170,11 @@ void test_search(){ assert(v.search(14, 9, v.getSize()) == 8); assert(v.search(1, 0, v.getSize()) == -1); + //test fib_search + assert(v.fib_search(14, 0, v.getSize()) == 7); + assert(v.fib_search(14, 9, v.getSize()) == 8); + assert(v.fib_search(1, 0, v.getSize()) == -1); + //test binary_search assert(v.binary_search(14, 0, v.getSize()) == 7); assert(v.binary_search(15, 0, v.getSize()) == 7); @@ -177,6 +189,33 @@ void test_search(){ cout << "test passed." << endl; } +void test_bubbleSort(){ + cout << "run test_bubbleSort...\n"; + int A[] = { 2,3,4,6,1,0,9,8,7,5 }; + Vector v(A, 10); + assert(v.unordered() == 5); + + v.bubbleSort(); + assert(v.unordered() == 0); + for (int ix = 0; ix != 10; ++ix) + assert(v[ix] == ix); + + srand((unsigned)time(NULL)); + Vector v1; + for (int ix = 0; ix != ELEMENT_NUM; ++ix) + v1.push_back(rand()); + assert(v1.getSize() == ELEMENT_NUM); + assert(v1.unordered() != 0); + + clock_t begin = clock(); + v1.bubbleSort(); + clock_t end = clock(); + assert(v.unordered() == 0); + cout << "Running time: " << end - begin << endl; + + cout << "test passed." << endl; +} + template void Increase(T & elem) { ++elem; } void test_map(){