资料库

数据结构课程设计(排序综合).doc

第1页 / 共13页
第2页 / 共13页
第3页 / 共13页
第4页 / 共13页
第5页 / 共13页
第6页 / 共13页
第7页 / 共13页
第8页 / 共13页
资料共13页,剩余部分请下载后查看
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
if(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((j
j++;
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(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--)
if(a[j+1]
{
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
}
}
}
1 问题要求及任务描述 1.1 题目要求 利用随机函数产生 N 个随机整数(20000 以上),对这些数进行多种方法进行排序。 要求: 1)至少采用三种方法实现上述问题求解(提示,可采用的方法有插入排序、希尔排序、起 泡排序、快速排序、选择排序、堆排序、归并排序)。并把排序后的结果保存在不同的文件 中。 2)统计每一种排序方法的性能(以上机运行程序所花费的时间为准进行对比),找出其中 两种较快的方法。 3)如果采用 4 种或 4 种以上的方法者,可适当加分。 1.2 主要任务 分别实现直接插入、直接选择、冒泡、快速排序、堆排序的算法。从时间的角度来分 析各种排序的性能。通过测试多组数据来掌握各种排序的方法及适用场合,并能在解决实际 问题灵活运用。在编写代码的时候,有以下几个问题: (1)建立一个主函数,在主函数中要有菜单界面,和输入功能键相应执行的功能。并 且要求能循环使用系统。 (2)分别实现直接插入、直接选择、冒泡、快速排序、堆排序的算法。 (3)通过冒泡排序法来测试每组数据用那种排序算法最优。 2 解决问题的主要思路和方法 2.1 关键问题 核心问题: 排列组合 数据模型(逻辑结构):30000 个随机数 存储结构: 保存在不同的文件 核心算法: 直接插入、直接选择、冒泡、快速排序、堆排序的算法 输入数据: 初始化数组:rand()%50000+1 输出数据:排序内容到文件,排序所用时间 2.2 拟采用解决问题的方法 把程序分为多个模块,有的是排序的算法如:void InsertSort(int a[],int p),有的是计 算排序所用时间的函数如:double TInsertSort(int a[],int p),还有显示菜单的模块 void menu()和显示排序结果模块 void Disp(int a[])等等,各个模块之间可以互相调用,节省 了 资 源 。 用 case 作 为 各 个 功 能 的 入 口 。 用 QueryPerformanceFrequency ( ) 和 QueryPerformanceCounte()函数计时,精度比用 clock()高,避免了很多时候因排序速 度太快而出现运行时间为 0 的现象。用 srand 函数作为随机数发生器的初始化函数,用 rand
产生随机数。把计算得的排序时间存入数组并经冒泡排序得出最快的两种排序法。 下面是所用排序法的算法思想: (1)直接插入排序的基本思想是基于插入,开始假定第一个记录有序,然后从第二个记录 开 始, 依 次插 入 到前 面 有序 的 子 文件 中 。即 将 记录 a[i](2<=i<=n)插 入到 有 序子 序列 a[1..i-1]中,使记录的有序序列从 a[1..i-1]变为 a[1..i] ,最终使整个文件有序。共进 行 n-1 趟插入。最坏时间复杂度是 0(n2),平均时间复杂度是 0(n2),空间复杂度是 O(1), 是稳定排序。 (2)直接选择排序的基本思想是基于选择,开始有序序列长度为零,第 i(1<=i
开始 显示菜单 1 直接 插入 排序 2 直接 选择 排序 3 输入序号 4 冒泡 排序 快速 排序 5 堆 排序 6 时间 效率 比较 显示排序后的的数据和时间效率 显示各个排序法对同一组 数据排序所用的时间和其 中两种较快的方法 7 显示 随机 数 0 退 出 结束 3 程序实现 3.1 程序实现时应考虑的问题
void InsertSort(int a[],int p) void SelectSort(int a[],int p) void BubbleSort(int a[],int p) void heapsort(int a[],int n,int p) void Disp(int a[]) creatheap(int void a[],int i,int n) quicksort(int void a[],int n, int p) double TInsertSort(int a[],int p) double TBubbleSort(int a[],int p) double Tquicksort(int a[],int left, int right,int p) double TSelectSort(int a[],int p) void BubleSort(double a[]) 时 间 数 组 的 冒 泡 排 序 double Theapsort(int a[],int n,int p) 模块化能使节约资源,也方便了程序的调试和增加功能。 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
printf("%-7d",a[i]); } } void InsertSort(int a[],int p) //插入排序 { int i,j,temp; for(i=1;i0&&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
int i,j,temp; for (i=0;ii;j--) /*比较,找出本趟最小关键字的记录*/ if (a[j]=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(lowtemp)j--; if(i
{ int i; int b[N]; for(i=0;i
分享到:
收藏