- 7
- 0
- 约8.1千字
- 约 19页
- 2021-04-06 发布于天津
- 举报
—
— PAGE # —
—
— PAGE # —
PAGE
PAGE #
北京信息科技大学
课程设计报告
课程名称 数据结构课程设计
题 目 排序与查找
指导教师 赵庆聪
设计起止日期
设计地点
系 另寸 信息管理学院
专 业 信息管理与信息系统
姓名/学号 鲁丹2012012108
1.课程实践目的:
通过本实践使学生对各类排序算法有更深入的了解, 在实际应用中学会使用排序算法解决具体
问题。
2.课程实践内容:
a)随机产生20个0—100之间的整数,允许有重复
b)分别利用直接插入排序、直接选择排序、快速排序、双向起泡排序对这20个数进行排序(递
增递减均可),并统计在各种排序方法中关键字的 比较次数,最后输出各类排序方法的排序]
结果及关键字的比较次数。
提示:双向起泡排序是对标准起泡排序算法的改进,该方法第一次自上而下进行“起泡” ,
使最大元素“下沉”到底,第二次自下而上进行“起泡” ,使最小元素“上浮”到顶,之后又重复
上述过程,每趟起泡后都会相应缩小下一趟的起泡排序区间,直至排序完成。起泡期间可以通过对 某趟“起泡”的“最后交换位置”进行记忆,以尽可能快地缩短下一趟的“起泡”区间。
c)用折半查找法在前面的已排好序的数据表上查找,是否有此数,如有,输出其序号。如没
有,在屏幕给出提示信息。
3.实践步骤:
#in cludestdio.h
#in cludestdlib.h
#in cludetime.h
#define N 100
#defi ne OK 1
#defi ne ERROR 0
#defi ne LIST_INIT_SIZE 100
#defi ne LISTINCREMENT 10
#defi ne INFEASIBLE -1 #defi ne OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct{
ElemType *elem;
int len gth;
int listsize;
}List;
Status InitList(List L)
{
L.elem=(ElemType * ) malloc(LIST_INIT_SIZE * sizeof(ElemType));
L.le ngth = 0;
L.listsize二LIST_INIT_SIZE;
retur n OK;
}//lni tList
void Create(List L, int n)
{
int i;
sra nd(time(NULL));
for(i=0;i n ;i++)
{
L.elem[i]=ra nd()%N;
L」en gth++;
prin tf(%d , L.elem[i]);
}
prin tf(\n);
}
int InsertSort(List L)
{
int i,j,t,m;
m=0;
for(i=1;iL」en gth;i++)
{
t=L.elem[i];
j=i-1;
if(t=L.elem[j])
PAGE
PAGE # —
—
— PAGE # —
m++;
else
m++;
while((j=0)( tL.elem[j])) {
L.elem[j+1]=L.elem[j]; j--;
}
L.elem[j+1]=t;
}
return m;
}
int SelectSort(List L)
{
int i,j,k,min,t=0;
for(i=0;iL」en gth;i++)
{
min 二i;
for(j=i+1;jL」en gth;j++)
if(L.elem[j]vL.elem[mi n])
{
min二j;
t++;
}
else t++;
if(mi n!二i)
{
k=L.elem[i];
L.elem[i]=L.elem[mi n];
L.elem[mi n]=k;
}
}
return t;
}
int QuickSort(List L,int s,int t)
{
int i=s,j=t,co un t4=0;
{
L.elem[O]=L.elem[s];
do
{
while(jiL.elem[j]=L.elem[O])
{
j--;
coun t4++;
}
if(ij)
{
L.elem[i]=L.elem[j];
i++;
}
while(ijL.elem[i]v=L.elem[O])
{
i++;
coun t4++;
if(ivj) {
L.elem[j]二L.elem[i];
j--;
}
}
while(ivj);
L.elem[i]=L.elem[O];
QuickSort(L,s,j-1);
QuickSort(L,j+1,t
原创力文档

文档评论(0)