1选择排序及其对数器验证
#include<iostream>
#include<vector>
#include<algorithm>
#include<ctime>
using namespace std;
void selectsort(vector<int> &arr)
{
if (arr.empty() || arr.size() < 2)
return;
for (int i = 0; i < arr.size() - 1; i++)
{
int minindex = i;
for (int j = i + 1; j < arr.size(); j++)
{
minindex = arr[minindex] > arr[j] ? j : minindex;
}
swap(arr[i], arr[minindex]);
}
}
//对数器验证
void compare(vector<int>& arr)
{
sort(arr.begin(), arr.end());
}
//生成一个随机大的数组
vector<int> generateRandomArray(int maxsize, int maxvalue)
{
int size = rand() % (maxsize + 1);
vector<int> arr(size);
for (int i = 0; i < size; i++)
{
arr[i] = rand() % (maxvalue + 1) - rand() % (maxvalue + 1);
}
return arr;
}
bool isequal(vector<int> a, vector<int> b)
{
return a == b;
}
void print(vector<int> a)
{
for (int num : a)
{
cout << num << ' ';
}
cout << endl;
}
int main()
{
srand(0);
int testtime = 50;
int maxsize = 100;
int maxvalue = 100;
bool succeed = true;
for (int i = 0; i < testtime; i++)
{
vector<int> arr1 = generateRandomArray(maxsize, maxvalue);
vector<int> arr2 = arr1;
selectsort(arr1);
compare(arr2);
if (!isequal(arr1, arr2))
{
succeed = false;
print(arr1);
print(arr2);
break;
}
}
cout <<(succeed? "Nice!" : "Fucking fucked!" )<< endl;
}
2冒泡排序及其对数器验证
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void bubblesort(vector<int> &arr)
{
if (arr.empty() || arr.size() < 2)
return;
for (int e = arr.size() - 1; e > 0; e--) {
for (int i = 0; i < e; i++) {
if (arr[i] > arr[i + 1]) {
swap(arr[i], arr[i + 1]);
}
}
}
}
//对数器验证
void compare(vector<int>& arr)
{
sort(arr.begin(), arr.end());
}
//生成一个随机大的数组
vector<int> generateRandomArray(int maxsize, int maxvalue)
{
int size = rand() % (maxsize + 1);
vector<int> arr(size);
for (int i = 0; i < size; i++)
{
arr[i] = rand() % (maxvalue + 1) - rand() % (maxvalue + 1);
}
return arr;
}
bool isequal(vector<int> a, vector<int> b)
{
return a == b;
}
void print(vector<int> a)
{
for (int num : a)
{
cout << num << ' ';
}
cout << endl;
}
int main()
{
srand(0);
int testtime = 50;
int maxsize = 100;
int maxvalue = 100;
bool succeed = true;
for (int i = 0; i < testtime; i++)
{
vector<int> arr1 = generateRandomArray(maxsize, maxvalue);
vector<int> arr2 = arr1;
bubblesort(arr1);
compare(arr2);
if (!isequal(arr1, arr2))
{
succeed = false;
print(arr1);
print(arr2);
break;
}
}
cout <<(succeed? "Nice!" : "Fucking fucked!" )<< endl;
}