二分法查找实验报告_第1页
二分法查找实验报告_第2页
二分法查找实验报告_第3页
二分法查找实验报告_第4页
二分法查找实验报告_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、数据结构二分法查找实验班级:计算机应用技术10-1班 学号:10602101018姓名:石美远一、实验目的及要求:掌握有关二分法函数对数据的统计的基本操作和存储结构,并编写相应的基本操 作算法。二、实验内容:1、完成插入排序函数的程序2、用循环完成二分法查找函数3、用递归完成二分法查找函数4、在二分法函数中统计出查找次数与被查找关键字在有序表中的位置5、最好能与顺序查找对比查找效率三、实验准备:计算机设备;程序调试环境的准备,如VC6、C+或WIN-TC环境;实验内容的算法分析与代码设计准备。四、函数实现1、二分法函数#include#define MAXSIZE 10int BinSearc

2、h(int s,int low, int high, int key) int mid,count=0;while(lowsmid) 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(lowsmid) return BinSearch2(s,mid+1,high,key);elsereturn BinSearch2(s,low,mid-1,key);return -1;int main() int mMAXSI

3、ZE = 4,7,-1,0,8,12,20,3,2,15,i,length,key,location;for(length=1;length=0) if(keymi) mi+1=mi;i-;else break;mi+1=key;printf(nnSorted:);for(i=0;i10;i+)printf(%3d ”,mi);printf(nn);location=BinSearch(m,0,9,7);printf(nt location=%dn”,location);location=BinSearch2(m,0,9,12);printf(nt location=%dn”,location

4、);return 0;2、二分法函数统计数据#include#define MAXSIZE 10int BinSearch(int s,int low, int high, int key) int mid,count=0;while(lowsmid) low=mid+1;else high=mid-1;printf(nt Search fail!n);return -1;int BinSearch2(int s,int low, int high, int key) int mid;static int count=0;while(lowsmid) return BinSearch2(s,m

5、id+1,high,key);elsereturn BinSearch2(s,low,mid-1,key);printf(nt Search fail!n);return -1;void InserSort(int m,int length) int key,i,k;for (k=1;k=0) if(keymi) mi+1=mi;i-;else break;mi+1=key;int main() int mMAXSIZE = 4,7,-1,0,8,12,20,3,2,15,location,len=10,x,i;InserSort(m,len);printf(nnSorted:);for(i=

6、0;i10;i+)printf(%3d ”,mi);printf(nn Input the search data:);scanf(%d”,&x);location=BinSearch(m,0,len-1,x);printf(nt location=%dn,location);location=BinSearch2(m,0,len-1,x);printf(nt location=%dn,location);return 0;五、测试(在主函数中调用函数测试功能)#include#include#define MAXSIZE 80int main(void) char p1=,p2=;print

7、f(nt (1)p1-length=%d p2-length=%dn,StrLen(p1),StrLen(p2);Strcat(p1,p2);printf(nt (2)p1=%sn,p1);printf(nt (3)the result is %dn,Strcmp(p1,p2) );ReverseStr(pl);printf(nt (4)p1=%sn,p1);getch();return 0;实验截图:c: C:Documents and SettingsAdministrator桌面、二分法RDebug6.EKeSorted: -10234?8121520count=3location=6count=2location=8Press any key to continuec: C:Document5 and SettingsAdministrator桌面、二分法KDebugBinsearch.eKeaiSorted: -10234?8121520Input the search data:7count=3location=6count=3location=6Press any key to continue六、心得体会通过这次的实验,我又学到了有关二分法函数的调用的很多的知识,从这个函

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论