1 问题要求及任务描述
1.1 题目要求
1.2 主要任务
2 解决问题的主要思路和方法
2.1 关键问题
输出数据:排序内容到文件,排序所用时间
2.2 拟采用解决问题的方法
把程序分为多个模块,有的是排序的算法如:void InsertSort(int a[],int p)
下面是所用排序法的算法思想:
(6)堆排序基本思想是:堆是n个元素的序列,先建堆,即先选得一个关键字最大或最小的记录,然后与序列中
2.3 主要算法和处理流程图
3 程序实现
3.1 程序实现时应考虑的问题
模块化能使节约资源,也方便了程序的调试和
3.2 主要源代码及说明
#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;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--) /*比较,找出本趟最小关键字的记录*/
{
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((j
j++;
{
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(i
while(i
if(i
}
a[i]=temp;
top++;st[top].low=low;st[top].high=
top++;st[top].low=i+1;st[top].high=
}
}
}
double TInsertSort(int a[],int p)
{
int i;
int b[N];
for(i=0;i
b[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
time/=m_liPerfFreq.QuadPart;
if(p!=6)
{Disp(b);getchar();}
printf("\n用直接插入排序法用的时间为%f秒;",time);
FILE *fp;
fp=fopen("直接插入排序.txt","w");
for(i=0;i
fprintf(fp,"%d ",b[i]);
fclose(fp);
return(time);
}
double TSelectSort(int a[],int p)
{
int i;
int b[N];
for(i=0;i
b[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
time/=m_liPerfFreq.QuadPart;
printf("\n用直接选择排序法用的时间为%f秒;",time);
FILE *fp;
fp=fopen("直接选择排序.txt","w");
for(i=0;i
fprintf(fp,"%d ",b[i]);
fclose(fp);return(time);
}
double TBubbleSort(int a[],int p)
{
int i;
int b[N];
for(i=0;i
b[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
time/=m_liPerfFreq.QuadPart;
if(p!=6)
{Disp(b);getchar();}
printf("\n用冒泡排序法用的时间为%f秒;",time);
FILE *fp;
fp=fopen("冒泡排序.txt","w");
for(i=0;i
fprintf(fp,"%d ",b[i]);
fclose(fp);return(time);
}
double Theapsort(int a[],int n,int p)
{
int i;
int b[N];
for(i=0;i
b[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
time/=m_liPerfFreq.QuadPart;
if(p!=6)
{Disp(b);getchar();}
printf("\n用堆排序法用的时间为%f秒;",time);
FILE *fp;
fp=fopen("堆排序.txt","w");
for(i=0;i
fprintf(fp,"%d ",b[i]);
fclose(fp);return(time);
}
double Tquicksort(int a[],int n,int p)
{
int i;
int b[N];
for(i=0;i
b[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
time/=m_liPerfFreq.QuadPart;
if(p!=6)
{Disp(b);getchar(); }
printf("\n用快速排序法用的时间为%f秒;",time);
FILE *fp;fp=fopen("快速排序.txt","w");
for(i=0;i
fprintf(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--)
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
void menu()
{
printf(" ★☆
printf(" ☆★*******************************
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请按任意键继续...");
case 2:TSelectSort(a,p);printf("\n请按任意键继续...");
case 3:TBubbleSort(a,p);printf("\n请按任意键继续...");
case 4:Tquicksort(a,N,p);printf("\n请按任意键继续...")
case 5:Theapsort(a,N,p);printf("\n请按任意键继续...");
case 6:system("cls");
TIMES1[1]=TIMES[1]=TInsertSort(a,p);TIMES1[2]=TIME
TIMES1[3]=TIMES[3]=TBubbleSort(a,p);TIMES1[4]=
BubleSort(TIMES);
printf("\n\n");
{
printf("排序这组数据两种较快的排序法分别是:\n");
if(TIMES[1]==TIMES1[1]) printf("直接插入排序:%f秒!\n",
if(TIMES[1]==TIMES1[2]) printf("直接选择排序:%f
if(TIMES[1]==TIMES1[3]) printf("冒泡排序:%f秒!\n",TI
if(TIMES[1]==TIMES1[4]) printf("快速排序:%f秒!\n",TI
if(TIMES[1]==TIMES1[5]) printf("堆排序:%f秒!\n",TIM
if(TIMES[1]!=TIMES[2])
{
if(TIMES[2]==TIMES1[1]) printf("直接插入排序:%f
if(TIMES[2]==TIMES1[2]) printf("直接选择排序%f秒
if(TIMES[2]==TIMES1[3]) printf("冒泡排序%f秒!\n",TIM
if(TIMES[2]==TIMES1[4]) printf("快速排序%f秒!\n",TIM
if(TIMES[2]==TIMES1[5]) printf("堆排序%f秒!\n",TIME
}
} printf("\n请按任意键继续...");srand((int)time(NULL));
for(i=0;i
case 7:Disp(a);FILE *fp;fp=fopen("随机数.txt","w");
for(i=0;i
default:Wrong();printf("\n请按任意键继续...");getchar();b
}
}
}