#include<iostream.h>
#include<time.h>
#include<math.h>
#include<iomanip.h>
#include <stdlib.h>
//外部变量定义
int count1=0,bj1=0,yd1=0;
int count2=0,bj2=0,yd2=0;
int count3=0,bj3=0,yd3=0;
int count4=0,bj4=0,yd4=0;
int count5=0,bj5=0,yd5=0;
int count6=0,bj6=0,yd6=0;
clock_t start1,finish1;
clock_t start2,finish2;
clock_t start3,finish3;
clock_t start4,finish4;
clock_t start5,finish5;
clock_t start6,finish6;
int b[6]={finish1-start1,finish2-start2,finish3-start3,finish4-start4,finish5-start5,finish6-start6};
template<class T>
class an
{
public:
void selectsort(T A[],int n);//简单选择排序
void insertsort(T A[],int n);//直接插入排序
void shellsort(T A[],int n); //希尔排序
void bubblesort(T A[],int n);//冒泡排序
void quicksort(T A[],int n);//快速排序
void mergesort(T A[],int n);//两路合并排序
private:
void merge(T A[],int i1,int j1,int i2,int j2);
void qsort(T A[],int left,int right);
};
template<class T>
void an<T>::selectsort(T A[],int n) //简单选择排序
{
int small;
start3=clock();
for(int i=0;i<n-1;i++)
{
small=i;
for(int j=i+1;j<n;j++)
{
if(A[j]<A[small])
{
small=j;
bj3++;
}
swap(A[i],A[small]);
count3++;
yd3+=3;
}
}
cout<<"简单选择排序后的数组是:"<<endl;
for(i=0;i<n;i++)
cout<<A[i]<<" ";
cout<<endl;
finish3=clock();
}
template<class T> //直接插入排序
void an<T>::insertsort(T A[],int n)
{
start1=clock();
for(int i=1;i<n;i++)
{
int j=i;
T temp=A[i];
while(j>0 && temp<A[j-1])
{
bj1++;
A[j]=A[j-1];
j--;
yd1++;
}
A[j]=temp;
}
cout<<"直接插入排序后的数组是:"<<endl;
for(i=0;i<n;i++)
cout<<A[i]<<" ";
cout<<endl;
finish1=clock();
}
template<class T> //希尔排序
void an<T>::shellsort(T A[],int n)
{
start2=clock();
int j,tmp,jmp;
jmp=n/2;
while(jmp!=0)
{
for(int i=jmp;i<n;i++)
{
tmp=A[i];
j=i-jmp;
while(tmp<A[j]&&j>=0)
{
bj2++;
A[j+jmp]=A[j];
yd2++;
j=j-tmp;
}
bj2++;
A[jmp+j]=tmp;
yd2++;
}
jmp=jmp/2;
}
cout<<"希尔排序后的数组是:"<<endl;
for(int i=0;i<n;i++)
cout<<A[i]<<" ";
cout<<endl;
finish2=clock();
}
template<class T> //冒泡排序
void an<T>::bubblesort(T A[],int n)
{
start4=clock();
int i=n-1,j,last;
while(i>0)
{
last=0;
for(j=0;j<i;j++,i--)
{
if(A[j+1]<A[j])
{
bj4++;
swap(A[j],A[j+1]);
count4++;
yd4+=3;
last=j;
}
bj4++;
}
i=last;
}
cout<<"冒泡排序后的数组是:"<<endl;
for(i=0;i<n;i++)
cout<<A[i]<<" ";
cout<<endl;
finish4=clock();
}
template<class T> //快速排序
void an<T>::quicksort(T A[],int n)
{
start5=clock();
qsort(A,0,n-1);
cout<<"快速排序后的数组是:"<<endl;
for(int i=0;i<n;i++)
cout<<A[i]<<" ";
cout<<endl;
finish5=clock();
}
template<class T>
void an<T>::qsort(T A[],int left,int right)
{
int i,j;
if(left<right)
{
i=left;
j=right+1;
do{
do{
i++;
bj5++;
}while(A[i]<A[left]);
do {
j--;
bj5++;
}while(A[j]>A[left]);
if(i<j)
{
swap(A[i],A[j]);count5++;yd5+=3;
}
}while(i<j);
swap(A[left],A[j]);
yd5+=3;
count5++;
qsort(A,left,j-1);
qsort(A,j+1,right);
}
}
template<class T> //两路合并排序
void an<T>::merge(T A[],int i1,int j1,int i2,int j2)
{
T *temp=new T[j2-i1+1];
int i=i1,j=i2,k=0;
while(i<=j1&&j<=j2)
if(A[i]<=A[j])
{
temp[k++]=A[i++];bj6++;yd6++;
}
else
{
temp[k++]=A[j++];bj6++;yd6++;
}
while(i<=j1)
{
temp[k++]=A[i++];bj6++;yd6++;
}
while(j<=j2)
{
temp[k++]=A[j++];yd6++;
}
for(i=0;i<k;i++)
{
A[i1++]=temp[i];yd6++;
}
delete []temp;
}
template<class T>
void an<T>::mergesort(T A[],int n)
{
start6=clock();
int i1,j1,i2,j2;
int size=1;
while(size<n)
{
i1=0;
while(i1+size<n)
{
i2=i1+size;
j1=i2-1;
if(i2+size-1>n-1)
j2=n-1;
else j2=i2+size-1;
merge(A,i1,j1,i2,j2);
i1=j2+1;
}
size*=2;
}
cout<<"两路合并排序后的数组是:"<<endl;
for(int i=0;i<n;i++)
cout<<A[i]<<" ";
cout<<endl;
finish6=clock();
}
template<class T>
void swap(T &a,T &b)
{
int c;
c=a;
a=b;
b=c;
}
void main()
{
an<int> p;
int rand( void ); //生成函数rand 播种子的srand
int n,ch,x=78;
int *array;
cout<<endl;
cout<<endl;
cout<<" * 欢 * 迎 * 进 * 入 * 内 * 排 * 序 * 比 * k较 * 系 * 统 * "<<endl;
cout<<endl;
cout<<" 请输入排序个数n!!!!"<<endl;
cin>>n;
array=new int[n];
for(int i=0;i<n;i++) array[i]=rand()%n+1;
cout<<"随机生成的原数组为:"<<endl;
for(i=0;i<n;i++) cout<<array[i]<<" ";
cout<<endl;
do{
cout<<"请选择排序方法或查看各种排序算法的性能比较!!!!"<<endl;
cout<<"<0> 退出"<<endl;
cout<<"<1> 直接插入排序"<<endl;
cout<<"<2> 希尔排序"<<endl;
cout<<"<3> 简单选择排序"<<endl;
cout<<"<4> 冒泡排序"<<endl;
cout<<"<5> 快速排序"<<endl;
cout<<"<6> 两路合并排序"<<endl;
cout<<"<7> 查看各种排序算法的性能比较"<<endl;
cin>>ch;
switch(ch){
case 1:
p.insertsort(array,n);
cout<<"排序算法名称为:直接插入排序! "<<endl;
cout<<"时间复杂度o(n^2)为:O("<<n*n<<")"<<endl;
cout<<"数据量大小(多少个)为:"<<n<<endl;
cout<<"比较次数为:"<<bj1<<endl;
cout<<"平均移动次数为:"<<yd1<<endl;
cout<<"交换次数为:"<<count1<<endl;
cout<<"实际执行时间为:"<<finish1-start1<<endl;
break;
case 2:
p.shellsort(array,n);
cout<<"排序算法名称为:希尔排序! "<<endl;
cout<<"时间复杂度O(n*log(2*n))为:O("<<int(n*log(2*n))<<")"<<endl;
cout<<"数据量大小(多少个)为:"<<n<<endl;
cout<<"比较次数为:"<<bj2<<endl;
cout<<"平均移动次数为:"<<yd2<<endl;
cout<<"交换次数为:"<<count2<<endl;
cout<<"实际执行时间为:"<<finish2-start2<<endl;
break;
case 3:
p.selectsort(array,n);
cout<<"排序算法名称为:简单选择排序! "<<endl;
cout<<"时间复杂度o(n^2)为:O("<<n*n<<")"<<endl;
cout<<"数据量大小(多少个)为:"<<n<<endl;
cout<<"比较次数为:"<<bj3<<endl;
cout<<"平均移动次数为:"<<yd3<<endl;
cout<<"交换次数为:"<<count3<<endl;
cout<<"实际执行时间为:"<<finish3-start3<<endl;
break;
case 4:
p.bubblesort(array,n);
cout<<"排序算法名称为:冒泡排序! "<<endl;
cout<<"时间复杂度o(n^2)为:O("<<n*n<<")"<<endl;
cout<<"数据量大小(多少个)为:"<<n<<endl;
cout<<"比较次数<为:"<<bj4<<endl;
cout<<"平均移动次数为:"<<yd4<<endl;
cout<<"交换次数为:"<<count4<<endl;
cout<<"实际执行时间为:"<<finish4-start4<<endl;
break;
case 5:
p.quicksort(array,n);
cout<<"排序算法名称为:快速排序! "<<endl;
cout<<"时间复杂度O(nlogn)为:O("<<int(n*log(n))<<")"<<endl;
cout<<"数据量大小(多少个)为:"<<n<<endl;
cout<<"比较次数为:"<<bj5<<endl;
cout<<"平均移动次数为:"<<yd5<<endl;
cout<<"交换次数为:"<<count5<<endl;
cout<<"实际执行时间为:"<<finish5-start5<<endl;
break;
case 6:
p.mergesort(array,n);
cout<<"排序算法名称为: 两路合并排序! "<<endl;
cout<<"时间复杂度O(nlog2n)为:O("<<int(n*log(2*n))<<")"<<endl;
cout<<"数据量大小(多少个)为:"<<n<<endl;
cout<<"比较次数为:"<<bj6<<endl;
cout<<"平均移动次数为:"<<yd6<<endl;
cout<<"交换次数为:"<<count6<<endl;
cout<<"实际执行时间为:"<<finish6-start6<<endl;
break;
case 7:
p.insertsort(array,n);
p.shellsort(array,n);
p.selectsort(array,n);
p.bubblesort(array,n);
p.quicksort(array,n);
p.mergesort(array,n);
cout<<" 排序算法名称|"<<setw(6)<<"时间复杂度|"<<setw(6)<<"数据量|"<<setw(6)<<"比较次数|"<<setw(6)<<"移动次数|"<<setw(6)<<"交换次数|"<<setw(6)<<"执行时间|"<<endl;
cout<<endl;
cout<<"<1> 直接插入排序:"<<setw(6)<<"o("<<n*n<<")"<<setw(6)<<n<<setw(8)<<bj1<<setw(9)<<yd1<<setw(8)<<count1<<setw(6)<<finish1-start1<<setw(4)<<endl;
cout<<"<2> 希尔排序:"<<setw(10)<<"o("<<int(n*log(2*n))<<")"<<setw(8)<<n<<setw(6)<<bj2<<setw(9)<<yd2<<setw(8)<<count2<<setw(8)<<finish2-start2<<endl;
cout<<"<3> 简单选择排序:"<<setw(6)<<"o("<<n*n<<")"<<setw(6)<<n<<setw(6)<<bj3<<setw(10)<<yd3<<setw(9)<<count3<<setw(6)<<finish3-start3<<endl;
cout<<"<4> 冒泡排序:"<<setw(10)<<"o("<<n*n<<")"<<setw(6)<<n<<setw(6)<<bj4<<setw(8)<<yd4<<setw(8)<<count4<<setw(9)<<finish4-start4<<endl;
cout<<"<5> 快速排序:"<<setw(10)<<"o("<<int(n*log(n))<<")"<<setw(8)<<n<<setw(8)<<bj5<<setw(6)<<yd5<<setw(8)<<count5<<setw(9)<<finish5-start5<<endl;
cout<<"<6> 两路合并排序:"<<setw(6)<<"o("<<int(n*log(n))<<")"<<setw(8)<<n<<setw(6)<<bj6<<setw(9)<<yd6<<setw(8)<<count6<<setw(8)<<finish6-start6<<endl;
cout<<endl;
break;
case 0:
x=0;
break;
}
}while(x);
}