📄 4sort_time.cpp
字号:
#include <iostream>
#include <vector>
#include "d_timer.h"
#include "d_random.h"
#include "d_sort.h"
using namespace std;
enum sortType{Selection,Insertion,Bubble,Exchange,Shell};
enum testType{Rand,Ascending,Descending};
void timeSort(sortType stype,testType tst,int vecSize)
{
int i;
timer t;
randomNumber rnd;
vector<int> v(vecSize);
for(i=0;i<vecSize;i++)
{
if(tst==Rand)
v[i]=rnd.random(100*vecSize)+1;
else if(tst==Ascending)
v[i]=i+1;
else if(tst==Descending)
v[i]=vecSize-i;
}
switch(stype)
{
case Selection: t.start();selectionSort(v);t.stop();
cout<<"Selection sort takes "<<t.time()<<" seconds"<<endl;
break;
case Insertion: t.start();insertionSort(v);t.stop();
cout<<"Insertion sort takes "<<t.time()<<" seconds"<<endl;
break;
case Bubble: t.start();bubbleSort(v); t.stop();
cout<<"Bubble sort takes "<<t.time()<<" seconds"<<endl;
break;
case Exchange: t.start();exchangeSort(v);t.stop();
cout<<"Exchange sort takes "<<t.time()<<" seconds"<<endl;
break;
case Shell: t.start();shellSort(v);t.stop();
cout<<"Shell sort takes "<<t.time()<<" seconds"<<endl;
break;
}
}
void main()
{
int i,j,m;
sortType sort;
testType test;
cout<<"Please input one sort: Selection-0,Insertion-1,Bubble-2,Exchange-3,Shell-4,End-5";
cin>>i;
while(i!=5)
{
switch(i)
{
case 0:
sort=Selection; break;
case 1:
sort=Insertion; break;
case 2:
sort=Bubble;break;
case 3:
sort=Exchange;break;
case 4:
sort=Shell;break;
}
cout<<"Input vecSize"<<endl;
cin>>m;
cout<<"Rand-0,Ascending-1,Descending-2"<<endl;
cin>>j;
switch(j)
{
case 0:
test=Rand;break;
case 1:
test=Ascending;break;
case 2:
test=Descending;break;
}
timeSort(sort,test,m);
cout<<"Please input one sort: Selection-0,Insertion-1,Bubble-2,Exchange-3,Shell-4,End-5";
cin>>i;
}
cout<<"bye!";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -