综合排序(c语音数据结构)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include
#include
#include
#include
#include
#define N 30000

void Wrong()
{
printf("\n=====>按键错误!\n");
getchar();
}
void Disp(int a[])
{
int i;
system("cls");
for(i=0;i{
if((i-1)%10==9)
printf("\n");
printf("%-7d",a[i]);

}
}

void InsertSort(int a[],int p) //插入排序
{
int i,j,temp;

for(i=1;i{
temp=a[i];
for(j=i;j>0&&a[j-1]>temp;j--)
a[j]=a[j-1];
a[j]=temp;
}

}

void SelectSort(int a[],int p) //选择排序
{

int i,j,k;

for(i=0;i{
k=i;
for(j=i+1;jif(a[j]k=j;
if(k!=i)
{
int temp;
temp=a[k];
a[k]=a[i];
a[i]=temp;
}
}

}

void BubbleSort(int a[],int p) /*冒泡排序算法*/
{

int i,j,temp;

for (i=0;i{
for (j=N-1;j>i;j--) /*比较,找出本趟最小关键字的记录*/
if (a[j]{
temp=a[j]; /*进行交换,将最小关键字记录前移*/
a[j]=a[j-1];
a[j-1]=temp;
}
}

}
void creatheap(int a[],int i,int n) //创建堆
{
int j;
int t;
t=a[i];
j=2*(i+1)-1;
while(j<=n)
{
if((jj++;
if(t{
a[i]=a[j];
i=j;
j=2*(i+1)-1;
}
else
j=n+1;
}
a[i]=t;
}
void heapsort(int a[],int n,int p) //堆排序

{

int i;
int t;

for(i=n/2-1;i>=0;i--)
creatheap(a,i,n-1);
for(i=n-1;i>=1;i--)
{
t=a[0];
a[0]=a[i];
a[i]=t;
creatheap(a,0,i-1);}

}


void quicksort(int a[],int n,int p)
{

int i,j,low,high,temp,top=-1;

struct node
{
int low,high;
}st[N];
top++;
st[top].low=0;st[top].high=n-1;
while(top>-1)
{ low=st[top].low;high=st[top].high;
top--;
i=low;j=high;
if(low{ temp=a[low];
while(i!=j)
{ while(itemp)j--;
if(iwhile(iif(i}
a[i]=temp;
top++;st[top].low=low;st[top].high=i-1;
top++;st[top].low=i+1;st[top].high=high;
}
}
}




double TInsertSort(int a[],int p)
{
int i;
int b[N];
for(i=0;ib[i]=a[i];
LARGE_INTEGER m_liPerfFreq={0};
QueryPerformanceFrequency(&m_liPerfFreq);
LARGE_INTEGER m_liPerfStart={0};
QueryPerformanceCounter(&m_liPerfStart);
InsertSort(b,p);
LARGE_INTEGER liPerfNow={0};
QueryPerformanceCounter(&liPerfNow);
double time=liPerfNow.QuadPart - m_liPerfStart.QuadPart;
time/=m_liPerfFreq.QuadPart;
if(p!=6)
{Disp(b);getchar();}
printf("\n用直

接插入排序法用的时间为%f秒;",time);
FILE *fp;
fp=fopen("直接插入排序.txt","w");

for(i=0;ifprintf(fp,"%d ",b[i]);

fclose(fp);
return(time);
}
double TSelectSort(int a[],int p)
{
int i;
int b[N];
for(i=0;ib[i]=a[i];
LARGE_INTEGER m_liPerfFreq={0};
QueryPerformanceFrequency(&m_liPerfFreq);
LARGE_INTEGER m_liPerfStart={0};
QueryPerformanceCounter(&m_liPerfStart);
SelectSort(b,p);
if(p!=6)
{Disp(b);getchar();}
LARGE_INTEGER liPerfNow={0};
QueryPerformanceCounter(&liPerfNow);
double time=liPerfNow.QuadPart - m_liPerfStart.QuadPart;
time/=m_liPerfFreq.QuadPart;
printf("\n用直接选择排序法用的时间为%f秒;",time);

FILE *fp;
fp=fopen("直接选择排序.txt","w");

for(i=0;ifprintf(fp,"%d ",b[i]);
fclose(fp);return(time);
}

double TBubbleSort(int a[],int p)
{
int i;
int b[N];
for(i=0;ib[i]=a[i];
LARGE_INTEGER m_liPerfFreq={0};
QueryPerformanceFrequency(&m_liPerfFreq);
LARGE_INTEGER m_liPerfStart={0};
QueryPerformanceCounter(&m_liPerfStart);
BubbleSort(b,p);
LARGE_INTEGER liPerfNow={0};
QueryPerformanceCounter(&liPerfNow);
double time=liPerfNow.QuadPart - m_liPerfStart.QuadPart;
time/=m_liPerfFreq.QuadPart;
if(p!=6)
{Disp(b);getchar();}
printf("\n用冒泡排序法用的时间为%f秒;",time);

FILE *fp;
fp=fopen("冒泡排序.txt","w");

for(i=0;ifprintf(fp,"%d ",b[i]);
fclose(fp);return(time);
}
double Theapsort(int a[],int n,int p)
{
int i;
int b[N];
for(i=0;ib[i]=a[i];
LARGE_INTEGER m_liPerfFreq={0};
QueryPerformanceFrequency(&m_liPerfFreq);
LARGE_INTEGER m_liPerfStart={0};
QueryPerformanceCounter(&m_liPerfStart);
heapsort(b,N,p);
LARGE_INTEGER liPerfNow={0};
QueryPerformanceCounter(&liPerfNow);
double time=liPerfNow.QuadPart - m_liPerfStart.QuadPart;
time/=m_liPerfFreq.QuadPart;
if(p!=6)
{Disp(b);getchar();}
printf("\n用堆排序法用的时间为%f秒;",time);

FILE *fp;
fp=fopen("堆排序.txt","w");

for(i=0;ifprintf(fp,"%d ",b[i]);
fclose(fp);return(time);
}

double Tquicksort(int a[],int n,int p)
{
int i;
int b[N];
for(i=0;ib[i]=a[i];
LARGE_INTEGER m_liPerfFreq={0};
QueryPerformanceFrequency(&m_liPerfFreq);
LARGE_INTEGER m_liPerfStart={0};
QueryPerformanceCounter(&m_liPerfStart);
quicksort(b,N,p);
LARGE_INTEGER liPerfNow={0};
QueryPerformanceCounter(&liPerfNow);
double time=liPerfNow.QuadPart - m_liPerfStart.QuadPart;
time/=m_liPerfFreq.QuadPart;
if(p!=6)
{Disp(b);getchar(); }
printf("\n用快速排序法用的时间为%f秒;",time);

FILE *fp;fp=fopen("快速排序.txt","w");
for(i=0;ifprintf(fp,"%d ",b[i]);

fclose(fp); return(time);

}

void BubleSort(double a[]) //时间数组的冒泡排序
{
int i,j;
double temp;
for

(i=1;i<6;i++)
{
for(j=4;j>=i;j--)
if(a[j+1]{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;

}


}
}

void menu()
{
printf(" ★☆************** 欢迎来到综合排序系统! ****************★☆\n");
printf(" ☆★ ☆★\n");
printf(" ★☆ ★☆\n");
printf(" ☆★ ☆★\n");
printf(" ★☆ ★☆\n");
printf(" ☆★ ☆★\n");
printf(" ☆★ ※※※※※※※ ☆★\n");
printf(" ★☆ ※ 菜 单 ※ ★☆\n");
printf(" ☆★ ※※※※※※※ ☆★\n");
printf(" ☆★ ☆★\n");
printf(" ★☆************** (1)---直接插入排序 ******************★☆\n");
printf(" ☆★************** (2)---直接选择排序 ******************☆★\n");
printf(" ★☆************** (3)---冒泡排序 ******************★☆\n");
printf(" ☆★************** (4)---快速排序 ******************☆★\n");
printf(" ★☆************** (5)---堆排序 ******************★☆\n");
printf(" ☆★************** (6)---时间效率比较 ******************☆★\n");
printf(" ★☆************** (7)---显示随机数 ******************★☆\n");
printf(" ☆★************** (0)---退出 ******************☆★\n");
printf(" ★☆ ★☆\n");
printf(" ☆★****************************************************☆★\n");
printf("\n====>请在上述序号中选择一个并输入:");
}

void main()
{
int i,p,a[N];


srand((int)time(NULL)); /*随机种子*/

for(i=0;i
a[i]=rand()%50000+1;
while(1)
{
system("cls");
menu();
scanf("%d",&p);
if(p==0)
{
printf("=====>谢谢使用!\n");
getchar();
break;
}
double TIMES[5],TIMES1[5];//时间数组

switch(p)
{
case 1:TInsertSort(a,p);printf("\n请按任意键继续...");getchar();break;
case 2:TSelectSort(a,p);printf("\n请按任意键继续...");getchar();break;
case 3:TBubbleSort(a,p);printf("\n请按任意键继续...");getchar();break;
case 4:Tquicksort(a,N,p);printf("\n请按任意键继续...");getchar();break;
case 5:Theapsort(a,N,p);printf("\n请按任意键继续...");getchar();break;
case 6:system("cls");
TIMES1[1]=TIMES[1]=TInsertSort(a,p);TIME

S1[2]=TIMES[2]=TSelectSort(a,p);
TIMES1[3]=TIMES[3]=TBubbleSort(a,p);TIMES1[4]=TIMES[4]=Tquicksort(a,N,p);TIMES1[5]=TIMES[5]=Theapsort(a,N,p);getchar();
BubleSort(TIMES);
printf("\n\n");
{
printf("排序这组数据两种较快的排序法分别是:\n");
if(TIMES[1]==TIMES1[1]) printf("直接插入排序:%f秒!\n",TIMES[1]);
if(TIMES[1]==TIMES1[2]) printf("直接选择排序:%f秒!\n",TIMES[1]);
if(TIMES[1]==TIMES1[3]) printf("冒泡排序:%f秒!\n",TIMES[1]);
if(TIMES[1]==TIMES1[4]) printf("快速排序:%f秒!\n",TIMES[1]);
if(TIMES[1]==TIMES1[5]) printf("堆排序:%f秒!\n",TIMES[1]);
if(TIMES[1]!=TIMES[2])
{
if(TIMES[2]==TIMES1[1]) printf("直接插入排序:%f秒!\n",TIMES[2]);
if(TIMES[2]==TIMES1[2]) printf("直接选择排序%f秒!\n",TIMES[2]);
if(TIMES[2]==TIMES1[3]) printf("冒泡排序%f秒!\n",TIMES[2]);
if(TIMES[2]==TIMES1[4]) printf("快速排序%f秒!\n",TIMES[2]);
if(TIMES[2]==TIMES1[5]) printf("堆排序%f秒!\n",TIMES[2]);
}
} printf("\n请按任意键继续...");srand((int)time(NULL));
for(i=0;icase 7:Disp(a);FILE *fp;fp=fopen("随机数.txt","w");
for(i=0;idefault:Wrong();printf("\n请按任意键继续...");getchar();break;
}
}
}

相关文档
最新文档