网络工程数据结构综合排序课程设计报告免费

发布时间:2019-09-21 18:32:16

《数据结构》

课程设计报告

指导教师

起止时间 ______

课程设计排序综合

一、任务描述

1至少采用三种方法实现上述问题求解(提示,可采用的方法有插入排序、希尔排序、冒泡排序、快速排序、选择排序、堆排序、归并排序)。并把排序后的结果保存在不同的文件中。

2统计每一种排序方法的性能(以上机运行程序所花费的时间为准进行对比),找出其中两种较快的方法。

二、问题分析

1功能分析

分析设计课题的要求,要求编程实现以下功能:

1)显示随机数:调用Dip()函数输出数组a[]。数组a[]中保存有随机产生的随机数。

2)直接选择排序:通过n-I次关键字间的比较,从n-i+1个记录中选出关键字最小的记录,并和第i个记录交换之。

3)冒泡排序:如果有n个数,则要进行n-1趟比较。在第1趟比较中要进行n-1次两两比较,在第j趟比较中要进行n-j次两两比较。

4)希尔排序:先将整个待排记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录“基本有序”时,再对全体记录进行一次直接插入排序。

5)直接插入排序:将一个记录插入到已排序好的有序表中,从而得到一个新的、记录数增1的有序表。设整个排序有n个数,则进行n-1趟插入,即:先将序列中的第1个记录看成是一个有序的子序列,然后从第2个记录起逐个进行插入,直至整个序列变成按关键字非递减有序列为止。

6显示各排序算法排序后的的数据和时间效率,并比较找出其中2种较快的方法。

2、数据对象分析

排序方式:直接选择排序、冒泡排序、希尔排序、直接插入排序

显示排序后的的数据和时间效率

三、数据结构设计

1.主要全程变量及数据结构

数据结构:

typedef struct

{

KeyType key;

InfoType otherinfo;

}RedType;

typedef struct

{

RedType r[MAXSIZE+1];

int length;

}SqList;

2.算法的入口参数及说明

#include

#define MAXSIZE 20

#define LT(a,b) ((a)<(b)) //宏定义

typedef int KeyType; //定义关键字KeyTypeint

typedef int InfoType; //定义关键字InfoTypeint

typedef struct{ //RedType结构定义

KeyType key;

InfoType otherinfo; //记录中其他信息域的类型

}RedType;

typedef struct{ //SqList结构定义

RedType r[MAXSIZE+1]; //定义大小

int length; //length为待排记录个数

}SqList;

四、功能设计

(一)主控菜单设计

为实现排序的操作功能,首先设计一个含有多个菜单项的主控菜单程序,然后再为这些菜单项配上相应的功能。

程序运行后,给出11个菜单项的内容和输入提示,如下:

欢迎来到排序综合系统!

菜单

(1)---直接插入排序

(2)---直接选择排序

(3)---冒泡排序

(4)---快速排序

(5)---堆排序

(6)---时间效率比较

(7)---显示随机数

(0)---退出系统

请在上述序号中选择一个并输入:

(二)程序模块结构

由课题要求可将程序划分为以下几个模块(即实现程序功能所需的函数):

主控菜单项选择函数menu_select()

插入排序函数:InsertSort()

选择排序函数:SelectSort()

冒泡排序函数:BubbleSort()

堆排序函数:heapsort()

(三)函数调用关系

程序的主要结构(函数调用关系)如图所示

其中main()是主函数,它进行菜单驱动,根据选择项1~0调用相应的函数。

(四)函数实现

#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=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;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.QuadPart;

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.QuadPart;

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.QuadPart;

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.QuadPart;

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.QuadPart;

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(" 欢迎来到排序综合系统! \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 请在上述序号中选择一个并输入: ");

}

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);TIMES1[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;i

case 7:Disp(a);FILE *fp;fp=fopen("随机数.txt","w");

for(i=0;i请按任意键继续...");getchar();break;

default:Wrong();printf("\n请按任意键继续...");getchar();break;

}

}

}

五、测试数据和结果

本程序在VC++环境下实现,下面是对以上测试数据的运行结果。

(1) 菜单显示如下

(2)各分界面:

主菜单

测试

结果

六、结束语

这次的数据结构课程设计中,排序综合,通过该题目的设计过程,加深了对排序算法的理解,对排序算法上基本运算的实现有所掌握,对课本中所学的各种数据结构进一步理解和掌握,学会了如何把学到的知识用于解决实际问题,锻炼了自己动手的能力。

网络工程数据结构综合排序课程设计报告免费

相关推荐