add interpolation_search & mergeSort as well as corresponding tests

This commit is contained in:
Shine wOng
2019-05-13 19:38:47 +08:00
parent 47d0086d38
commit b283bec82f
2 changed files with 115 additions and 0 deletions

View File

@@ -107,6 +107,18 @@ void Vector<T>::swap(T &one, T &two){
two = temp;
}
template <typename T>
void Vector<T>::merge(int lo, int mid, int hi){
int leftLen = mid - lo;
T* tmp = new T[leftLen];
for (int ix = 0; ix != leftLen; ++ix) tmp[ix] = _elem[ix + lo];
for (int pos = lo, ix = 0, jx = mid; ix != leftLen;) {
if (hi <= jx || tmp[ix] <= _elem[jx]) _elem[pos++] = tmp[ix++];
if (jx < hi && _elem[jx] < tmp[ix]) _elem[pos++] = _elem[jx++];
}
}
template <typename T>
int Vector<T>::bubble(int lo, int hi){
int lastSwap = 0;
@@ -189,6 +201,20 @@ int Vector<T>::binary_search(T const &elem, int lo, int hi){
return lo - 1;
}
template <typename T>
int Vector<T>::interpolation_search(T const &elem, int lo, int hi){
int mid;
while(lo < hi){
mid = lo + (elem - _elem[lo])*(hi - lo) / (_elem[hi] - _elem[lo]);
if (mid < lo || mid >= hi) break;
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>::unordered(int lo, int hi){
int reversed = 0;
@@ -286,4 +312,14 @@ void Vector<T>::bubbleSort(int lo, int hi){
while (lo < (hi = bubble(lo, hi)));
}
template <typename T>
void Vector<T>::mergeSort(int lo, int hi){
if (hi - lo < 2) return;
int mid = (lo + hi) >> 1;
mergeSort(lo, mid);
mergeSort(mid, hi);
merge(lo, mid, hi);
}
#endif

View File

@@ -17,8 +17,10 @@ void test_delete();
void test_find();
void test_unique();
void test_search();
void test_search_performance();
void test_map();
void test_bubbleSort();
void test_mergeSort();
int main(){
test_constructor();
@@ -28,7 +30,9 @@ int main(){
test_find();
test_unique();
test_search();
test_search_performance();
test_bubbleSort();
test_mergeSort();
test_map();
system("pause");
@@ -175,6 +179,11 @@ void test_search(){
assert(v.fib_search(14, 9, v.getSize()) == 8);
assert(v.fib_search(1, 0, v.getSize()) == -1);
//test interpolation_search
assert(v.interpolation_search(14, 0, v.getSize()) == 7);
assert(v.interpolation_search(14, 9, v.getSize()) == 8);
assert(v.interpolation_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);
@@ -189,6 +198,49 @@ void test_search(){
cout << "test passed." << endl;
}
void test_search_performance(){
cout << "run test_search_performance..." << endl;
srand((unsigned)time(NULL));
Vector<int> v1;
for (int ix = 0; ix != ELEMENT_NUM; ++ix)
v1.push_back(rand());
v1.mergeSort();
//test binary search
cout << "run binary_search...";
clock_t begin = clock();
for(int ix = 0; ix != NUMOFLOOPS; ++ix)
assert(v1.binary_search(v1[824], 0, v1.getSize()) == 824);
clock_t end = clock();
cout << "Running time: " << end - begin << endl;
//test plain old search
cout << "run plain old search...";
begin = clock();
for (int ix = 0; ix != NUMOFLOOPS; ++ix)
assert(v1.search(v1[824], 0, v1.getSize()) == 824);
end = clock();
cout << "Running time: " << end - begin << endl;
//test fib search
cout << "run fib_search...";
begin = clock();
for (int ix = 0; ix != NUMOFLOOPS; ++ix)
assert(v1.fib_search(v1[824], 0, v1.getSize()) == 824);
end = clock();
cout << "Running time: " << end - begin << endl;
//test interpolation search
cout << "run binary_search...";
begin = clock();
for (int ix = 0; ix != NUMOFLOOPS; ++ix)
assert(v1.interpolation_search(v1[824], 0, v1.getSize()) == 824);
end = clock();
cout << "Running time: " << end - begin << endl;
cout << "test passed." << endl;
}
void test_bubbleSort(){
cout << "run test_bubbleSort...\n";
int A[] = { 2,3,4,6,1,0,9,8,7,5 };
@@ -216,6 +268,33 @@ void test_bubbleSort(){
cout << "test passed." << endl;
}
void test_mergeSort(){
cout << "run test_mergeSort..." << endl;
int A[] = { 2,3,4,6,1,0,9,8,7,5 };
Vector<int> v(A, 10);
assert(v.unordered() == 5);
v.mergeSort();
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.mergeSort();
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(){