数据结构课程设计排序与查找.docx

上传人:b****5 文档编号:4060687 上传时间:2022-11-27 格式:DOCX 页数:17 大小:19.30KB
下载 相关 举报
数据结构课程设计排序与查找.docx_第1页
第1页 / 共17页
数据结构课程设计排序与查找.docx_第2页
第2页 / 共17页
数据结构课程设计排序与查找.docx_第3页
第3页 / 共17页
数据结构课程设计排序与查找.docx_第4页
第4页 / 共17页
数据结构课程设计排序与查找.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

数据结构课程设计排序与查找.docx

《数据结构课程设计排序与查找.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计排序与查找.docx(17页珍藏版)》请在冰豆网上搜索。

数据结构课程设计排序与查找.docx

数据结构课程设计排序与查找

北京信息科技大学

课程设计报告

 

课程名称数据结构课程设计

题目排序与查找

指导教师赵庆聪

设计起止日期

设计地点

 

系别信息管理学院

专业__信息管理与信息系统_

姓名/学号______鲁丹2012012108__

 

1.课程实践目的:

通过本实践使学生对各类排序算法有更深入的了解,在实际应用中学会使用排序算法解决具体问题。

2.课程实践内容:

a)随机产生20个0—100之间的整数,允许有重复

b)分别利用直接插入排序、直接选择排序、快速排序、双向起泡排序对这20个数进行排序(递增递减均可),并统计在各种排序方法中关键字的比较次数,最后输出各类排序方法的排序结果及关键字的比较次数。

提示:

双向起泡排序是对标准起泡排序算法的改进,该方法第一次自上而下进行“起泡”,使最大元素“下沉”到底,第二次自下而上进行“起泡”,使最小元素“上浮”到顶,之后又重复上述过程,每趟起泡后都会相应缩小下一趟的起泡排序区间,直至排序完成。

起泡期间可以通过对某趟“起泡”的“最后交换位置”进行记忆,以尽可能快地缩短下一趟的“起泡”区间。

c)用折半查找法在前面的已排好序的数据表上查找,是否有此数,如有,输出其序号。

如没有,在屏幕给出提示信息。

3.实践步骤:

#include

#include

#include

#defineN100

#defineOK1

#defineERROR0

#defineLIST_INIT_SIZE100

#defineLISTINCREMENT10

#defineINFEASIBLE-1

#defineOVERFLOW-2

typedefintStatus;

typedefintElemType;

typedefstruct{

ElemType*elem;

intlength;

intlistsize;

}List;

StatusInitList(List&L)

{

L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));

L.length=0;

L.listsize=LIST_INIT_SIZE;

returnOK;

}//InitList

voidCreate(List&L,intn)

{

inti;

srand(time(NULL));

for(i=0;i

{

L.elem[i]=rand()%N;

L.length++;

printf("%d",L.elem[i]);

}

printf("\n");

}

intInsertSort(ListL)

{

inti,j,t,m;

m=0;

for(i=1;i

{

t=L.elem[i];

j=i-1;

if(t>=L.elem[j])

m++;

else

m++;

while((j>=0)&&(t

{

L.elem[j+1]=L.elem[j];

j--;

}

L.elem[j+1]=t;

}

returnm;

}

intSelectSort(ListL)

{

inti,j,k,min,t=0;

for(i=0;i

{

min=i;

for(j=i+1;j

if(L.elem[j]

{

min=j;

t++;

}

elset++;

if(min!

=i)

{

k=L.elem[i];

L.elem[i]=L.elem[min];

L.elem[min]=k;

}

}

returnt;

}

intQuickSort(ListL,ints,intt)

{

inti=s,j=t,count4=0;

if(s

{

L.elem[0]=L.elem[s];

do

{

while(j>i&&L.elem[j]>=L.elem[0])

{

j--;

count4++;

}

if(i

{

L.elem[i]=L.elem[j];

i++;

}

while(i

{

i++;

count4++;

}

if(i

{

L.elem[j]=L.elem[i];

j--;

}

}

while(i

L.elem[i]=L.elem[0];

QuickSort(L,s,j-1);

QuickSort(L,j+1,t);

}

returncount4;

}

intBubbleSort(ListL)

{

intflag,i,j;

intt=0;

flag=1;

while(flag==1)

{

flag=0;

i=0;

for(j=L.length-i;j>i;j--)

{

if(L.elem[j-1]>L.elem[j])

{

flag=1;

intm;

m=L.elem[j];

L.elem[j]=L.elem[j-1];

L.elem[j-1]=m;

t++;

}

elset++;

}

}

returnt;

}

voiddisplay(ListL)

{

inti;

for(i=0;i

{

printf("%d",L.elem[i]);

}

printf("\n");

}

voidmain()

{

ListL;

intlow,high,a,b,c,d;

InitList(L);

printf("请将随机产生的0-100间的20个数输出:

\n");

Create(L,20);

printf("\n直接插入法排序输出的顺序表为:

\n");

a=InsertSort(L);

display(L);

printf("此排序法关键字比较的次数为:

%d\n",a);

printf("\n直接选择法排序输出的顺序表为:

\n");

b=SelectSort(L);

display(L);

printf("此排序法关键字比较的次数为:

%d\n",b);

printf("\n快速排序输出的顺序表为:

\n");

c=QuickSort(L,1,20);

display(L);

printf("此排序法关键字比较的次数为:

%d\n",c);

printf("\n双向起泡法排序输出的顺序表为:

\n");

d=BubbleSort(L);

display(L);

printf("此排序法关键字比较的次数为:

%d\n",d);

}

1.#include "stdio.h"   

2.#include "stdlib.h"   

3.#include "string.h"   

4.#include "time.h"   

5.#include "limits.h"   

6.#define MAXITEM 1000   

7.typedef int KeyType,ElemType;   

8.int count1=0,count2=0,count3=0,count4=0,count5=0,count6=0;   

9.int swap1=0,swap2=0,swap3=0,swap4=0,swap5=0,swap6=0;   

10.typedef struct rec   

11.{   

12.    KeyType key;   

13.    ElemType data;   

14.}sqlist[MAXITEM];   

15.   

16.void gennum(sqlist r,sqlist t,int n)   

17.{   

18.    int i;   

19.    srand((unsigned)time(NULL));   

20.    for(i=1;i<=n;i++)   

21.    {   t[i].key=rand()%100;   

22.        r[i].key=t[i].key;   

23.    }   

24.}   

25.   

26.void ini(sqlist r,sqlist t,int n)   

27.{   

28.    int i;   

29.    for(i=1;i<=n;i++)   

30.        r[i].key=t[i].key;   

31.}   

32.   

33.void BubbleSort(sqlist r,int n)//起泡法r[1]~r[n]   

34.{   

35.    int i,j;   

36.    struct rec w;   

37.    for(i=1;i<=n-1;i++)   

38.       for(j=n;j>=i+1;j--)   

39.       {   

40.          if(r[j].key

41.          {   

42.             w=r[j];   

43.             r[j]=r[j-1];   

44.             r[j-1]=w;   

45.             swap1++;   

46.          }   

47.          count1++;   

48.       }   

49.   

50.}   

51.   

52.   

53.void InsertSort(sqlist r,int n)//直接插入排序r[1]~r[n]   

54.{   

55.    int i,j;   

56.    for(i=2;i<=n;i++)   

57.    {   

58.        count2++;   

59.        r[0]=r[i];   

60.        j=i-1;   

61.        while(r[0].key

62.        {   

63.            r[j+1]=r[j];   

64.            j--;   

65.            count2++;   

66.            swap2++;   

67.        }   

68.        r[j+1]=r[0];   

69.        swap2++;   

70.    }   

71.}   

72.   

73.void  SelectSort(sqlist r,int n)//简单选择排序r[1]~r[n]   

74.{   

75.    int i,j,k;   

76.    struct rec temp;   

77.    for(i=1;i<=n-1;i++)   

78.    {   

79.        k=i;   

80.        for(j=i+1;j<=n;j++)   

81.            if(r[j].key

82.        if(i!

=k)   

83.        {   

84.            temp=r[i];   

85.            r[i]=r[k];   

86.            r[k]=temp;   

87.            swap3++;   

88.        }   

89.    }   

90.}   

91.   

92.void QuickSort(sqlist r,int s,int t)//快速排序r[s]~r[t],r[0]空出   

93.{   

94.    int i=s,j=t;   

95.    if(s

96.    {   

97.        r[0]=r[s];swap4++;   

98.        do   

99.        {   

100.            while(j>i&&r[j].key>=r[0].key){j--;count4++;}   

101.            if(i

102.            {   

103.                r[i]=r[j];   

104.                i++;   

105.                swap4++;   

106.            }   

107.            while(i

108.            if(i

109.            {   

110.                r[j]=r[i];   

111.                j--;   

112.                swap4++;   

113.            }   

114.        }while(i

115.        r[i]=r[0];   

116.        swap4++;   

117.        QuickSort(r,s,j-1);   

118.        QuickSort(r,j+1,t);   

119.    }   

120.}   

121.   

122.void ShellSort(sqlist r,int n)//希尔排序r[1]~r[n]   

123.{   

124.    int i,j,gap;   

125.    struct rec x;   

126.    gap=n/2;   

127.    while(gap>0)   

128.    {   

129.        for(i=gap+1;i<=n;i++)   

130.        {   

131.   

132.            j=i-gap;   

133.            while(j>0)   

134.              if(r[j].key>r[j+gap].key)   

135.              {   

136.                x=r[j];   

137.                r[j]=r[j+gap];   

138.                r[j+gap]=x;   

139.                j=j-gap;   

140.                count5++;   

141.                swap5++;   

142.              }   

143.              else   

144.              {   

145.                j=0;count5++;   

146.              }   

147.        }   

148.        gap=gap/2;   

149.    }   

150.}   

151.   

152.void sift(sqlist r,int l,int m)   

153.{   

154.    int i,j;   

155.    struct rec x;   

156.    i=l;   

157.    j=2*i;   

158.    x=r[i];   

159.    while(j<=m)   

160.    {   

161.        if(j

162.        if(x.key

163.        {   

164.            r[i]=r[j];   

165.            i=j;   

166.            j=2*i;   

167.            count6++;   

168.            swap6++;   

169.        }   

170.        else {j=m+1;count6++;}   

171.    }   

172.    r[i]=x;   

173.}   

174.void HeapSort(sqlist r,int n)//堆排序r[1]~r[n]   

175.{   

176.    int i;   

177.    struct rec m;   

178.    for(i=n/2;i>=1;i--)sift(r,i,n);   

179.       for(i=n;i>=2;i--)   

180.       {   

181.          m=r[i];   

182.          r[i]=r[1];   

183.          r[1]=m;   

184.          swap6++;   

185.          sift(r,1,i-1);   

186.       }   

187.}   

188.   

189.void main()   

190.{   

191.       

192.    int k,n,a;   

193.    sqlist r,t;   

194.    printf("                 ***************************************\n");   

195.    printf("                 *                                     *\n");   

196.    printf("                 *      * 内部排序算法的性能分析 *     *\n");   

197.    printf("                 *                                     *\n");   

198.    printf("                 ***************************************\n\n");   

199.   

200.    printf("-----------------*-------------------------------------*----------------\n");   

201.    printf("**是否执行程序**\n");   

202.    printf("(是) 按 1 键,   (否) 按 0 键\n");   

203.    printf(" 按键为:

");   

204.    scanf("%d",&a);   

205.    printf("-----------------*-------------------------------------*----------------\n");   

206.   

207.    while(a==1)   

208.    {printf("**请输入要排序的数据的个数:

");   

209.     scanf("%d",&n);   

210.       

211.     gennum(r,t,n);   

212.     printf("\n");   

213.   

214.     printf("**随机产生的最初顺序是:

\n");   

215.     for(k=1;k<=n;k++)   

216.    {   printf("%3d",t[k].key);   

217.        if(k%20==0)   

218.            printf("\n");   

219.    }   

220.     printf("\n");   

221.     BubbleSort(r,n);   

222.     printf("\n**排序之后的顺序是:

\n");   

223.     for(k=1;k<=n;k++)   

224.    {   printf("%3d",r[k].key);   

225.        if(k%20==0)   

226.            printf("\n");   

227.    }   

228.     printf("\n\n");   

229.     printf("-------------------------------*分析结果*-------------------------------\n\n");   

230.     printf("                              **起泡排序**\n");   

231.     printf("                     比较的次数是:

 %d,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 数学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1