#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<string.h>
#include<windows.h>
#pragma comment(lib,"winmm.lib")
#define TRUE 1
#define FALSE 0
#define SNUM 7
int comp[SNUM]={0},move[SNUM]={0};
void Insert(int a[],int n){//直接插入排序
int i,j,temp;
for(i=1;i<n;i++){
comp[0]++;
if(a[i]<a[i-1]){
temp=a[i];move[0]++;
for(j=i-1;j>=0&&temp<a[j];j--){
a[j+1]=a[j];move[0]++;comp[0]++;
}
a[j+1]=temp;move[0]++;
}
}
}
void Shell(int a[],int n){//希尔排序
int i,j,temp,dk;
for(dk=n/2;dk>0;dk=dk/2)
for(i=dk;i<n;i++){
comp[1]++;
if(a[i]<a[i-dk]){
temp=a[i];move[1]++;
for(j=i-dk;j>=0&&temp<a[j];j-=dk)
{a[j+dk]=a[j];move[1]++;comp[0]++;}
a[j+dk]=temp;move[1]++;
}
}
}
void Bubble(int a[],int n){ //冒泡排序
int temp,i,j;
int NOSWAP;
for(i=n-1;i>0;--i){
NOSWAP=TRUE;
for(j=0;j<i;j++){
comp[2]++;
if(a[j]>a[j+1]){
temp=a[j];a[j]=a[j+1];
a[j+1]=temp;NOSWAP=FALSE;
move[2]+=3;
}
}
if(NOSWAP)break;
}
}
void Quick(int a[],int low,int high){ //快速排序
int temp,L=low,H=high;
if(low<high){
temp=a[low];move[3]++; //用区间的第1个记录作为基准
while(low<high){
while(low<high&&a[high]>=temp)
{high--;comp[3]++;}
a[low]=a[high];move[3]++;
while(low<high&&a[low]<=temp)
{low++;comp[3]++;}
a[high]=a[low];move[3]++;
}
a[low]=temp; move[3]++; //low的值相当于pivotkey
Quick(a,L,low-1);//对左区间递归排序
Quick(a,low+1,H);//对右区间递归排序
}
}
void SelectSort(int a[],int n){ //简单选择排序
int i,j,k,tmp;
for(i=0;i<n-1;i++){
k=i;
for(j=i+1;j<n;j++){
comp[4]++;
if(a[j]<a[k])k=j;
}
if(i!=k){
tmp=a[i];a[i]=a[k];
a[k]=tmp;move[4]+=3;
}
}
}
void HeapAdjust(int a[],int s,int m){ //使H.r[s..m]成为一个大顶堆
int j,tmp;
tmp=a[s]; move[5]++;
for(j=2*s+1;j<=m;j*=2){ //j为s的左孩子,所以j=2*s+1
comp[5]++;
if(j<m&&a[j]<a[j+1])++j;
if(tmp>=a[j]) break;
a[s]=a[j];move[5]++;
s=j;
}
a[s]=tmp;
}
void HeapSort(int a[],int n){ //堆排序
int i,tmp;
for(i=n/2-1;i>=0;--i)
//从最后一个元素的下标j(=n-1)=2*i+1开始,所以i=n/2-1
HeapAdjust(a,i,n-1); //将初始待排记录建成一个大顶堆
for(i=n-1;i>0;--i){
tmp=a[i];a[i]=a[0];a[0]=tmp;
move[5]+=3;
HeapAdjust(a,0,i-1);
}
}
void Merge(int a[],int low,int m,int high)
{//将两个有序的子序列a[low..m)和a[m+1..high]归并成一个
//有序的子序列a[low..high]
int *b; //b为辅助数组基址
b=(int*)malloc((high-low+1)*sizeof(int));
for(int i=low,j=m+1,k=0;i<=m&&j<=high;++k){
//两子序列均非空时取其小者输出到b[k]上
if(a[j]<a[i])b[k]=a[j++];
else b[k]=a[i++];
move[6]++;comp[6]++;
}
while(i<=m) //若第1个子序列非空,则复制剩余记录到b中
{b[k++]=a[i++];move[6]++;}
while(j<=high) //若第2个子序列非空,则复制剩余记录到b中
{b[k++]=a[j++];move[6]++;}
for(k=0,i=low;i<=high;k++,i++,move[6]++)
a[i]=b[k];//归并完成后将结果复制回a[low..high]
free(b);
}
void MergeSort(int a[],int low,int high){
//用分治法对a[low..high]进行二路归并排序
int mid;
if(low<high){//区间长度大于1
mid=(low+high)/2;//分解
MergeSort(a,low,mid);//递归地对a[low..mid]排序
MergeSort(a,mid+1,high); //递归地对a[mid+1..high]排序
Merge(a,low,mid,high);//组合,将两个有序区归并为一个有序区
}
}
_int64 Runtime(int *b,int n,char TYPE){
int *a;
LARGE_INTEGER ft;
_int64 begin_t,end_t;
a=(int*)malloc(n*sizeof(int));
if(!a)exit(0);
for(int i=0;i<n;i++)a[i]=b[i];
QueryPerformanceCounter(&ft);
begin_t=ft.QuadPart;
switch(TYPE){
case'I':Insert(a,n);break;
case'S':Shell(a,n);break;
case'B':Bubble(a,n);break;
case'Q':Quick(a,0,n-1);break;
case'J':SelectSort(a,n);break;
case'H':HeapSort(a,n);break;
case'2':MergeSort(a,0,n-1);
}
QueryPerformanceCounter(&ft);
end_t=ft.QuadPart;
free(a);
return end_t-begin_t;
}
void Disp(int n,double e[]){
static num;
int s[SNUM];
for(int i=0;i<SNUM;i++)s[i]=comp[i]+move[i];
printf(" 第%d次测试结果 \n",num++);
printf("-----------------------------------------------------------\n");
printf("| 排序算法 | 比较次数 | 移动次数 | 总次数 | 耗费时间 | \n");
printf("-----------------------------------------------------------\n");
printf("|直接插入排序| %10u %10u %10u %6.2fms\n",comp[0],move[0],s[0],e[0]);
printf("| 希尔排序 | %10u %10u %10u %6.2fms\n",comp[1],move[1],s[1],e[1]);
printf("| 起泡排序 | %10u %10u %10u %6.2fms\n",comp[2],move[2],s[2],e[2]);
printf("| 快速排序 | %10u %10u %10u %6.2fms\n",comp[3],move[3],s[3],e[3]);
printf("|简单选择排序| %10u %10u %10u %6.2fms\n",comp[4],move[4],s[4],e[4]);
printf("| 堆排序 | %10u %10u %10u %6.2fms\n",comp[5],move[5],s[5],e[5]);
printf("|2-路归并排序| %10u %10u %10u %6.2fms\n",comp[6],move[6],s[6],e[6]);
printf("-----------------------------------------------------------\n");
}
void main()
{
int i,n,*b;
LARGE_INTEGER ft;
_int64 k,j,tmp,zhp;
double sum,e[SNUM]={0};
srand(time(NULL));
QueryPerformanceFrequency(&ft);
zhp=ft.QuadPart;
do{
QueryPerformanceCounter(&ft);
k=ft.QuadPart;
printf("输入待排序元素个数(输入0结束):");
scanf("%d",&n);
b=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)b[i]=rand()*rand()%100000;
tmp=Runtime(b,n,'I');
e[0]=(double)tmp/(double)zhp*1000;
tmp=Runtime(b,n,'S');
e[1]=(double)tmp/(double)zhp*1000;
tmp=Runtime(b,n,'B');
e[2]=(double)tmp/(double)zhp*1000;
tmp=Runtime(b,n,'Q');
e[3]=(double)tmp/(double)zhp*1000;
tmp=Runtime(b,n,'J');
e[4]=(double)tmp/(double)zhp*1000;
tmp=Runtime(b,n,'H');
e[5]=(double)tmp/(double)zhp*1000;
tmp=Runtime(b,n,'2');
e[6]=(double)tmp/(double)zhp*1000;
if(n>0)Disp(n,e);
QueryPerformanceCounter(&ft);
j=ft.QuadPart;
sum=(double)(j-k)/(double)zhp*1000;
printf("\n程序运行总时间为:%1.2fms\n",sum);
for(i=0;i<SNUM;i++)comp[i]=move[i]=0;//初始化
free(b);
}while(n>0);
system("pause");
}
温馨提示:答案为网友推荐,仅供参考