数据结构课程设计综合排序代码及实验报告书。
发布网友
发布时间:2022-04-19 22:57
我来回答
共2个回答
热心网友
时间:2023-10-04 06:59
#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 200
double maopao(int B[],int M)//冒泡排序
{
clock_t start, finish;
double time; // 测量一个事件持续的时间
start = clock();
FILE *fp1;
int i,j,t;
for(i=0;i<M-1;i++)//排序
for(j=0;j<M-i-1;j++)
if(B[j]>B[j+1])
{
t=B[j];B[j]=B[j+1];B[j+1]=t;
}
finish = clock();
time = (double)(finish - start) / CLOCKS_PER_SEC;
fp1=fopen("a","wb");
printf("经排序后序列为:\n");
for(i=0;i<M;i++)//输出排序后结果
{
if(i%10==0)printf("\n");
printf("%d ",B[i]);
fwrite(&B[i],sizeof(int),1,fp1);//把数据以二进制形式存入磁盘文件
}fclose(fp1);
printf("\n");
printf( "冒泡排序法运行时间:" );
printf( "%f seconds\n", time );
return time;
}
double charru(int A[], int len)//插入排序
{
clock_t start, finish;
double time; // 测量一个事件持续的时间
start = clock();
FILE *fp2;
int i,j,high,low,mid,temp;
for(i=1;i<len;i++)
{
temp=A[i];
low=0;high=i-1;
while(low<=high)
{
mid=(low+high)/2;
if(A[0]>temp)
high=mid-1;
else low=mid+1;
}
for(j=i-1;j>high;j--)
A[j+1]=A[j];
A[high+1]=temp;
}
finish = clock();
time = (double)(finish - start) / CLOCKS_PER_SEC;
fp2=fopen("b","wb");
printf("经排序后序列为:\n");
for(i=0;i<len;i++)
{
if(i%10==0)printf("\n");
printf("%d ",A[i]);
fwrite(&A[i],sizeof(int),1,fp2);
}fclose(fp2);
printf("\n");
printf( "插入排序法运行时间:" );
printf( "%f seconds\n", time );
return time;
}
double xuanze(int arry[],int n)//选择排序
{
clock_t start, finish;
double time; //测量一个事件持续的时间
start = clock();
FILE *fp3;
int i,j,k,t;
for(i=0;i<n;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(arry[j]<arry[k])
k=j;
t=arry[k];
arry[k]=arry[i];
arry[i]=t;
}
finish = clock();
time = (double)(finish - start) / CLOCKS_PER_SEC;
fp3=fopen("c","wb");
printf("经排序后序列为:\n");
for(i=0;i<n;i++)
{
if(i%10==0)printf("\n");
printf("%d ",arry[i]);
fwrite(&arry[i],sizeof(int),1,fp3);
} fclose(fp3);
printf("\n");
printf( "选择排序法运行时间:" );
printf( "%f seconds\n", time );
return time;
}
int kuaisu(int*c,int low,int high)//快速排序确定枢轴位置
{
int temp,t;
temp=c[low];
while(low<high)
{
while(low<high&&c[high]>=temp)high--;
{
t=c[low];c[low]=c[high];c[high]=t;
}
while(low<high&&c[low]<=temp)low++;
{
t=c[low];c[low]=c[high];c[high]=t;
}
}
return low;
}
void kuaisu1(int *b,int low,int high)//快速排序
{
int t;
if(low<high)
{
t=kuaisu(b,low,high);
kuaisu1(b,low,t-1);
kuaisu1(b,t+1,high);
}
}
double xier(int *x, int n)//希尔排序
{
clock_t start, finish;
double time; /* 测量一个事件持续的时间*/
start = clock();
FILE *fp5;
int h, j, k, t,i;
for(h=n/2; h>0; h=h/2)
{
for (j=h; j<n; j++)
{
t = *(x+j);
for(k=j-h; (k>=0 && t<*(x+k)); k-=h)
{
*(x+k+h) = *(x+k);
}
*(x+k+h) = t;
}
}
finish = clock();
time = (double)(finish - start) / CLOCKS_PER_SEC;
fp5=fopen("e","wb");
printf("经排序后序列为:\n");
for(i=0;i<n;i++)
{
if(i%10==0)
printf("\n");
printf("%d ",x[i]);
fwrite(&x[i],sizeof(int),1,fp5);
}fclose(fp5);
printf("\n");
printf( "希尔排序法运行时间:" );
printf( "%f seconds\n", time );
return time;
}
void main(void)
{
FILE *fp4;
int i,a[N];
double min,b[5];
char ch,ch1;
for(i=0;i<30;i++)
printf("*");
printf("欢迎进入综合排序系统");
for(i=0;i<30;i++)
printf("*");
printf("\n");
srand((unsigned)time(NULL));//获取随机数
printf("获得%d个随机数:\n",N);
for(i=0;i<N;i++)
a[i]=(rand()%12767+20000)%32767;
for(i=0;i<N;i++)//输出随机数
{
if(i%10==0)printf("\n");
printf("%d ",a[i]);
}
printf("\n");
do
{
for(i=0;i<80;i++)
printf("=");
printf("\t1.冒泡排序\n");
printf("\t2.选择排序\n");
printf("\t3.插入排序\n");
printf("\t4.快速排序\n");
printf("\t5.希尔排序\n");
printf("\t6.方法比较\n");
printf("\t7.退出\n");
printf("\t请选择排序方法:\n");
do
{
ch=getchar();
}while(ch!='1'&&ch!='2'&&ch!='3'&&ch!='4'&&ch!='5'&&ch!='6'&&ch!='7');
switch(ch)
{
case'1': b[0]=maopao(a,N);break;
case'2': b[1]=xuanze(a,N);break;
case'3': b[2]=charru(a,N);break;
case'4':
clock_t start, finish; //测量一个事件持续的时间
double time;
start = clock();
kuaisu1(a,0,N-1);
finish = clock();
time = (double)(finish - start) / CLOCKS_PER_SEC;
fp4=fopen("d","wb");
printf("经排序后序列为:\n");
for(i=0;i<N;i++)
{
if(i%10==0)printf("\n");
printf("%d ",a[i]);
fwrite(&a[i],sizeof(int),1,fp4);
}fclose(fp4);
printf("\n");
finish = clock();
time = (double)(finish - start) / CLOCKS_PER_SEC;
b[3]=time;
printf( "快速排序法运行时间:" );
printf( "%f seconds\n", time ); break;
case'5': b[4]=xier(a,N);break;
case'6': min=b[0];
for(i=0;i<5;i++)
{
if(min>b[i])
min=b[i];
}
ch1='a';
i=0;
while(i<5)
{
if(min>=b[i])
switch(ch1)
{
case 'a':printf("较好的算法是冒泡排序\n");break;
case 'b':printf("较好的算法是选择排序\n");break;
case 'c':printf("较好的算法是插入排序\n");break;
case 'd':printf("较好的算法是快速排序\n");break;
case 'e':printf("较好的算法是希尔排序\n");break;
}
ch1++;i++;
}break;
case'7': {
printf("\n");
for(i=0;i<37;i++)
printf("*");
printf("谢谢 !");
for(i=0;i<37;i++)
printf("*");
exit(0);
}
default:exit(1);
}
}while(1);
}
热心网友
时间:2023-10-04 07:00
需要对条目进行自动排序,当增加或减少条目时,下面的会自动进行再排序,你回答中的格式--样式和格式的操作,我不是很明白,要怎么操作才能实现自动排序。