#include <assert.h>
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
// swap two numbers in vector
template<typename T>
void swapNumber(vector<T>& vec, int i, int j) {
vec[i] = vec[i] ^ vec[j];
vec[j] = vec[i] ^ vec[j];
vec[i] = vec[i] ^ vec[j];
}
// compare two numbers
template<typename T>
bool compare(T a, T b) {
return a < b;
}
// bubble sort
// 1.要测的冒泡排序方法a
// 实现思路:每次只放好一个数(最右边的数最大)
// 伪代码:
// 0-end-1放好一个数
// 0-end-2放好一个数
// 0-end-3放好一个数
// ......
// 0-2放好一个数
// 0-1放好一个数
template<typename T>
void bubbleSort(vector<T>& vec) {
int length = vec.size();
if (length == 0 || length < 2) {
return;
}
for (int i = length - 1; i > 0; --i) {
for (int j = 0; j < i; ++j) {
if (vec[j] > vec[j + 1]) {
swapNumber(vec, j, j + 1);
}
}
}
}
//for test
//2.实现一个绝对正确即使复杂度不好的方法b
template<typename T> // absolute right because we use STL
void rightMethod(vector<T>& vec) {
sort(vec.begin(), vec.end(), compare<T>);
// sort(vec.begin(), vec.end(), std::less<T>()); // official
}
//for test
//3.实现一个随机样本产生器:生成容器大小随机和容器元素随机的容器
template<typename T>
void generateRandomVector(vector<T>& vec, int maxSize, T minValue, T maxValue) {
assert(maxValue > minValue); // assert
srand((unsigned int)time(nullptr)); // include<ctime>
int size = rand() % maxSize + 1; // [1, maxsize]
vec = vector<T>(size, -1); //initialization with -1
for (auto it = vec.begin(); it != vec.end(); ++it) {
*it = rand() % (maxValue - minValue) + minValue + 1; // [minvalue + 1, maxValue]
}
return;
}
//for test
//4.实现比对的方法:判断两个数组是否相等
template<typename T>
bool isEqual(vector<T>& vec1, vector<T>& vec2) {
if (vec1.size() != vec2.size()) {
return false;
}
else {
return equal(vec1.begin(), vec1.end(), vec2.begin()); // STL equal
}
return true;
}
//for test
template<typename T>
void copyVector(vector<T>& vec1, vector<T>& vec2) {
if (!vec2.size()) { // copy the vec2 to the vec1
return;
}
vec1 = vec2;
}
template<typename T>
void display(vector<T>& vec) {
for (auto it : vec) { // 取vec中的元素取出来copy到it中去,然后输出
cout << it << ",";
}
cout << "\n";
}
template<typename T> // here we use different functional pointer
void testAlgorithm(void (*sortFunc) (vector<T>&)) {
int testCount(100000); // epoch
int maxSize(10); // max size of the vector
int minValue(-100); // min value
int maxValue(100); // maxvalue
bool successFlag(true); // flag
clock_t startTime, endTime;
startTime = clock(); // start clock
for (int i = 0; i < testCount; ++i) {
vector<int> vec1, vec2, vec3;
generateRandomVector(vec1, maxSize, minValue, maxValue); // generate random vector
copyVector(vec2, vec1);
copyVector(vec3, vec1); // help to show
rightMethod(vec1); // absolute right algorithm
sortFunc(vec2); // we want to test the algorithm
if (!isEqual(vec1, vec2)) {
successFlag = false; // something wrong
display(vec3);
break;
}
// clean the usage of the vector, vector<T> ().swap()
vector<int>().swap(vec1);
vector<int>().swap(vec2);
vector<int>().swap(vec3);
}
if (successFlag) {
cout << "Nice!" << endl;
}
else {
cout << "There may be something wrong!" << endl;
}
endTime = clock();
cout << "Total usage is: " << (endTime - startTime) / double(CLOCKS_PER_SEC) << 's' << endl;
}
int main() {
testAlgorithm(bubbleSort<int>); // bubbleSort
system("pause");
return EXIT_SUCCESS;
}