#include"iostream"
#include"cstdio"
#include"stdlib.h"
#include"cstring"
#include"cstdlib"
#include"vector"
#include"map"
#include"algorithm"
#include <iomanip>
#include <time.h>
#include <fstream>
#include <sstream>
using namespace std;
typedef pair<string, double> PAIR;
struct CmpByValue
{
bool operator()(const PAIR& lhs, const PAIR& rhs)
{
return lhs.second < rhs.second;
}
};
const int M = 40000 ;
map<string,double>Map;
int mp[M];
void Random_number()
{
int RandomNumber;
srand((unsigned)time(NULL));
for(int i=0; i<M; i++)
{
RandomNumber = rand();
mp[i]=RandomNumber;
}
}
void quicksort(int left, int right, vector<int>& num)
{
if(left >= right)
return;
int i, j, base;
i = left, j = right;
base = num[left];
while (i < j)
{
while (num[j] >= base && i < j)
j--;
while (num[i] <= base && i < j)
i++;
if(i < j)
{
swap(num[i],num[j]);
}
}
num[left] = num[i];
num[i] = base;
quicksort(left, i - 1, num);
quicksort(i + 1, right, num);
}
void Quick_sort()
{
clock_t start,finish;
double duration;
vector<int>num;
FILE *fw;
if((fw=fopen("快速排序结果.txt","w+"))==NULL)
{
cout<<"Wrong read."<<endl;
exit(0);
}
for(int i=0; i<M; i++)
num.push_back(mp[i]);
start=clock();
quicksort(0,M-1,num);
finish=clock();
duration=(double)(finish - start)/CLOCKS_PER_SEC;
Map["快速排序"]=duration;
cout<<"快速排序所需时间:\t"<<duration<<endl;
for(int i=0; i<M; i++)
{
fprintf(fw,"%d ",num[i]);
}
fclose(fw);
}
void Selection_sort()
{
clock_t start,finish;
double duration;
int num[M];
FILE *fw;
if((fw=fopen("选择排序结果.txt","w+"))==NULL)
{
cout<<"Wrong read."<<endl;
exit(0);
}
for(int i=0; i<M; i++)
num[i]=mp[i];
start=clock();
for(int i=0; i<M; i++)
for(int j=i+1; j<M; j++)
{
if(num[i]>num[j])
{
swap(num[i],num[j]);
}
}
finish=clock();
duration=(double)(finish - start)/CLOCKS_PER_SEC;
Map["选择排序"]=duration;
cout<<"选择排序算法所需时间:\t"<<duration<<endl;
for(int i=0; i<M; i++)
{
fprintf(fw,"%d ",num[i]);
}
fclose(fw);
}
void Bubble_sort()
{
bool flag;
clock_t start,finish;
double duration;
int num[M];
FILE *fw;
if((fw=fopen("起泡排序结果.txt","w+"))==NULL)
{
cout<<"Wrong read."<<endl;
exit(0);
}
for(int i=0; i<M; i++)
num[i]=mp[i];
start=clock();
flag = true;
for(int i=1; i<M&&flag==true; i++)
{
flag=false;
for(int j=0; j<M-i; j++)
{
if(num[j]>num[j+1])
{
swap(num[j],num[j+1]);
flag =true;
}
}
}
finish=clock();
duration=(double)(finish - start)/CLOCKS_PER_SEC;
Map["起泡排序"]=duration;
cout<<"起泡排序算法所需时间:\t"<<duration<<endl;
for(int i=0; i<M; i++)
{
fprintf(fw,"%d ",num[i]);
}
fclose(fw);
}
void Insertion_sort()
{
clock_t start,finish;
double duration;
int num[M];
FILE *fw;
if((fw=fopen("插入排序结果.txt","w+"))==NULL)
{
cout<<"Wrong read."<<endl;
exit(0);
}
for(int i=0; i<M; i++)
num[i]=mp[i];
start=clock();
for(int i=1; i<M; i++)
{
for(int j=i; j>0; j--)
{
if(num[j-1]>num[j])
swap(num[j],num[j-1]);
}
}
finish=clock();
duration=(double)(finish - start)/CLOCKS_PER_SEC;
Map["插入排序"]=duration;
cout<<"插入排序所需时间:\t"<<duration<<endl;
for(int i=0; i<M; i++)
{
fprintf(fw,"%d ",num[i]);
}
fclose(fw);
}
void Shell_sotr()
{
clock_t start,finish;
double duration;
int num[M];
FILE *fw;
if((fw=fopen("希尔排序结果.txt","w+"))==NULL)
{
cout<<"Wrong read."<<endl;
exit(0);
}
for(int i=0; i<M; i++)
num[i]=mp[i];
start=clock();
int h=1;
while(h < M/3) h=3*h+1;
while(h>=1)
{
for(int i=h; i<M; i++)
{
for(int j=i; j>=h ; j-=h)
{
if(num[j-h]>num[j])
{
swap(num[j-h],num[j]);
}
}
}
h=h/3;
}
finish=clock();
duration=(double)(finish - start)/CLOCKS_PER_SEC;
Map["希尔排序"]=duration;
cout<<"希尔排序所需时间:\t"<<duration<<endl;
for(int i=0; i<M; i++)
{
fprintf(fw,"%d ",num[i]);
}
fclose(fw);
}
void Print()
{
cout<<"比较快的两种方法是:"<<endl;
vector<PAIR> SORT(Map.begin(),Map.end());
sort(SORT.begin(), SORT.end(), CmpByValue());
for(int i=0; i<2; i++)
{
cout<<"\t"<<SORT[i].first<<endl;
}
}
int main()
{
cout<<"程序已经运行,请稍等片刻"<<endl;
Random_number();
cout<<"随机数已生成完成"<<endl;
Selection_sort();
Bubble_sort();
Insertion_sort();
Shell_sotr();
Quick_sort();
Print();
return 0;
}