实验报告
姓 课程名称:
院(系 专业/年级:
数 据 结 构
二分法查找实验
班级:计算机应用技术10-1班 学号:xx 姓名:xx
一、实验目的及要求:
掌握有关二分法函数对数据的统计的基本操作和存储结构,并编写相应的基本操作算法。
二、实验内容:
1、完成插入排序函数的程序
2、用循环完成二分法查找函数
3、用递归完成二分法查找函数
4、在二分法函数中统计出查找次数与被查找关键字在有序表中的位置
5、最好能与顺序查找对比查找效率
三、实验准备:
1) 计算机设备;
2) 程序调试环境的准备,如VC6、C++或WIN-TC环境;
3) 实验内容的算法分析与代码设计准备。
四、函数实现
1、二分法函数
#include
#define MAXSIZE 10
int BinSearch(int s[],int low,int high,int key)
{ int mid,count=0;
while(low<=high)
{ mid=(low+high)/2;
count++;
if(key==s[mid])
{ printf("\n\t (1)count=%d\n",count);
return (mid+1);
}
else
if(key>s[mid]) low=mid+1;
else high=mid-1;
}
return -1;
}
int BinSearch2(int s[],int low,int high,int key)
{ int mid;
static int count=0;
while(low<=high)
{ mid=(low+high)/2;
count++;
if(key==s[mid])
{ printf("\n\t (2)count=%d\n",count);
return (mid+1);
}
else
if(key>s[mid]) return BinSearch2(s,mid+1,high,key);
else return BinSearch2(s,low,mid-1,key);
}
return -1;
}
int main()
{ int m[MAXSIZE]={4,7,-1,0,8,12,20,3,2,15},i,length,key,location;
for(length=1;length<10;length++)
{ key=m[length];
i=length-1;
while(i>=0)
{ if(key
else break;
}
m[i+1]=key;
}
printf("\n\nSorted:");
for(i=0;i<10;i++)
printf("%3d ",m[i]);
printf("\n\n");
location=BinSearch(m,0,9,7);
printf("\n\t location=%d\n",location);
location=BinSearch2(m,0,9,12);
printf("\n\t location=%d\n",location);
return 0;
}
2、二分法函数统计数据
#include
#define MAXSIZE 10
int BinSearch(int s[],int low,int high,int key)
{ int mid,count=0;
while(low<=high)
{ mid=(low+high)/2;
count++;
if(key==s[mid])
{ printf("\n\t (1)count=%d\n",count);
return (mid+1);
}
else
if(key>s[mid]) low=mid+1;
else high=mid-1;
}
printf("\n\t Search fail!\n");
return -1;
}
int BinSearch2(int s[],int low,int high,int key)
{ int mid;
static int count=0;
while(low<=high)
{ mid=(low+high)/2;
count++;
if(key==s[mid])
{ printf("\n\t (2)count=%d\n",count);
return (mid+1);
}
else
if(key>s[mid]) return BinSearch2(s,mid+1,high,key);
else return BinSearch2(s,low,mid-1,key);
}
printf("\n\t Search fail!\n");
return -1;
}
void InserSort(int m[],int length)
{ int key,i,k;
for(k=1;k
{ key=m[k];
i=k-1;
while(i>=0)
{ if(key
else break;
}
m[i+1]=key;
}
}
int main()
{ int m[MAXSIZE]={4,7,-1,0,8,12,20,3,2,15},location,len=10,x,i;
InserSort(m,len);
printf("\n\nSorted:");
for(i=0;i<10;i++)
printf("%3d ",m[i]);
printf("\n\n Input the search data:");
scanf("%d",&x);
location=BinSearch(m,0,len-1,x);
printf("\n\t location=%d\n",location);
location=BinSearch2(m,0,len-1,x);
printf("\n\t location=%d\n",location);
return 0;
}
五、测试(在主函数中调用函数测试功能)
#include
#include
#define MAXSIZE 80
int main(void)
{ char p1[]="",p2[]="";
printf("\n\t (1)p1->length=%d p2->length=%d\n",StrLen(p1),StrLen(p2));
Strcat(p1,p2);
printf("\n\t (2)p1=%s\n",p1);
printf("\n\t (3)the result is %d\n",Strcmp(p1,p2) );
ReverseStr(p1);
printf("\n\t (4)p1=%s\n",p1);
getch();
return 0;
}
实验截图:
六、心得体会
通过这次的实验,我又学到了有关二分法函数的调用的很多的知识,从这个函数里可以让我们很方便的统计数字。对这部分的知识我们有待加强,更好地去熟练这部分知识的应用。我们学习数据结构的时候还有很多的算法都还没有掌握,这次的实验给我们了很大的启示。所以去要在今后的学习中努力学号有关知识的相关内容,把我的基础打牢固。
实验十查找排序计算机学院12级2班1211020xx李龙实验目的1掌握折半查找算法的思想2实现折半查找的算法3掌握常见的排序算法插…
实验五查找与排序实验课程名数据结构与算法专业班级12级软件工程1班学号20xx40450149姓名刘浩实验时间61462134节实…
电子科技大学信息与软件工程学院实验报告电子科技大学实验报告课程名称学生姓名学号点名序号指导教师实验地点实验时间20xx20xx2学…
实验题91设计一个程序exp91cpp输出在顺序表36210185749中采用顺序方法找关键字5的过程程序如下文件名exp91cp…
信息检索课程结业报告姓学信息检索与web搜索应用背景及概念信息检索InformationRetrieval是指信息按一定的方式组织…
实验题91设计一个程序exp91cpp输出在顺序表36210185749中采用顺序方法找关键字5的过程程序如下文件名exp91cp…
学生成绩查询系统一实习任务2二系统分析3三系统设计4四调试排错测试试运行过程7五源程序完整或主要代码10六总结与体会17七参考文献…
附件2天津商业大学学生实验报告开课实验室403机房开课时间20xx年10月17日实验报告20xx年10月17日注1每个实验项目一份…