add Fib class, testFib.cpp, fib_search and bubbleSort

This commit is contained in:
Shine wOng
2019-05-13 14:05:00 +08:00
parent bc89fab071
commit 47d0086d38
5 changed files with 189 additions and 4 deletions

33
thu_dsa/chp2/Fib.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include "Fib.h"
#include <iostream>
using std::cout;
using std::endl;
//constructor
Fib::Fib(int n) {
elem = Vector<int>();
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();
}

18
thu_dsa/chp2/Fib.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef FIB_H_
#define FIB_H_
#include "Vector.h"
class Fib{
protected:
Vector<int> elem;
public:
//constructor
Fib(int n);
void print();
int get();
void prev();
};
#endif

View File

@@ -1,6 +1,7 @@
#ifndef VECTOR_H_
#define VECTOR_H_
#include "Fib.h"
#include <iostream>
#define DEFAULT_CAPACITY 3
@@ -21,6 +22,9 @@ protected:
void copyfrom(T* const A, int lo, int hi);
void copyfrom(Vector<T> 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<T>& operator=(Vector<T> 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 <typename VST> 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 <typename T>
void Vector<T>::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<T>::expand(){
if (_size < _capacity) return;
//else double capacity
T* tmp = _elem;
delete[]_elem;
copyfrom(tmp, 0, _size);
delete[]tmp;
}
template <typename T>
void Vector<T>::swap(T &one, T &two){
T temp;
temp = one;
one = two;
two = temp;
}
template <typename T>
int Vector<T>::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<T>::search(T const &elem, int lo, int hi){
}
return lo - 1;
}
template <typename T>
int Vector<T>:: 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 <typename T>
int Vector<T>::binary_search(T const &elem, int lo, int hi){
@@ -144,6 +189,14 @@ int Vector<T>::binary_search(T const &elem, int lo, int hi){
return lo - 1;
}
template <typename T>
int Vector<T>::unordered(int lo, int hi){
int reversed = 0;
while(++lo != hi){
if (_elem[lo] < _elem[lo - 1]) ++reversed;
}
return reversed;
}
// writable interfaces
template<typename T>
@@ -218,7 +271,7 @@ int Vector<T>::uniquify(){
}
template <typename T>
void Vector<T>::map(void(*visit)(T&)){
void Vector<T>::map(void(*visit)(T&)) {
for (int ix = 0; ix != _size; ++ix) visit(_elem[ix]);
}
@@ -227,4 +280,10 @@ void Vector<T>::map(VST& visit) {
for (int ix = 0; ix != _size; ++ix) visit(_elem[ix]);
}
//sorting algorithms
template <typename T>
void Vector<T>::bubbleSort(int lo, int hi){
while (lo < (hi = bubble(lo, hi)));
}
#endif

36
thu_dsa/chp2/testFib.cpp Normal file
View File

@@ -0,0 +1,36 @@
#include "Fib.h"
#include <cassert>
#include <iostream>
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;
}

View File

@@ -1,7 +1,12 @@
#include "Fib.h"
#include "Vector.h"
#include <time.h>
#include <cassert>
#include <iostream>
#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<int> 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<int> 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 <typename T>
void Increase(T & elem) { ++elem; }
void test_map(){