




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
5/28/2023数据结构与程序设计1数据结构与程序设计(19)5/28/2023数据结构与程序设计2BinarySearch
TheForgetfulVersionofBinarySearch
ForgetthepossibilitythattheKeytargetmightbefoundquicklyandcontinue,whethertargethasbeenfoundornot,tosubdividethelistuntilwhatremainshaslength1.5/28/2023数据结构与程序设计3BinarySearch
TheForgetfulVersionofBinarySearch
Idea:Insearchinganorderedlist,firstcomparethetargettothekeyinthecenterofthelist.Ifitissmaller,restrictthesearchtothelefthalf;otherwiserestrictthesearchtotherighthalf,andrepeat.Inthisway,ateachstepwereducethelengthofthelisttobesearchedbyhalf.Keeptwoindices,topandbottom,thatwillbracketthepartoftheliststilltobesearched.Thetargetkey,provideditispresent,willbefoundbetweentheindicesbottomandtop,inclusive.5/28/2023数据结构与程序设计4BinarySearch
TheForgetfulVersionofBinarySearchInitialization:Setbottom=0;top=the_list.size()−1;ComparetargetwiththeRecordatthemidpoint,mid=(bottom+top)/2;Changetheappropriateindextoporbottomtorestrictthesearchtotheappropriatehalfofthelist.Loopterminateswhentop<=bottom,ifithasnotterminatedearlierbyfindingthetarget.Makeprogresstowardterminationbyensuringthatthenumberofitemsremainingtobesearched,top−bottom+1,strictlydecreasesateachiterationoftheprocess.5/28/2023数据结构与程序设计5TheForgetfulVersionofBinarySearchP282Error_coderecursive_binary_1(constOrdered_list&the_list,constKey&target,intbottom,inttop,int&position){ Recorddata; if(bottom<top){//Listhasmorethanoneentry. intmid=(bottom+top)/2; the_list.retrieve(mid,data); if(target>data)//Reducetotophalfoflist. returnrecursive_binary_1(the_list,target,mid+1,top,position); else//Reducetobottomhalfoflist. returnrecursive_binary_1(the_list,target,bottom,mid,position); } elseif(top<bottom)//Canberemarked? returnnot_present;//Listisempty. else{//Listhasexactlyoneentry. position=bottom; the_list.retrieve(bottom,data); if(data==target)returnsuccess; elsereturnnot_present; }}5/28/2023数据结构与程序设计6TheForgetfulVersionofBinarySearchP283Error_codebinary_search_1(constOrdered_list&the_list,constKey&target,int&position){ Recorddata; intbottom=0,top=the_list.size()-1; while(bottom<top){ intmid=(bottom+top)/2; the_list.retrieve(mid,data); if(data<target) bottom=mid+1; else top=mid; } if(top<bottom)returnnot_present; else{ position=bottom; the_list.retrieve(bottom,data); if(data==target)returnsuccess; elsereturnnot_present; }}5/28/2023数据结构与程序设计7BinarySearch(RecognizingEquality)Examinestheelementinthemiddleofthearray.Isitthesoughtitem?Ifso,stopsearching.Isthemiddleelementtoosmall?Thenstartlookinginsecondhalfofarray.Isthemiddleelementtoolarge?Thenbeginlookinginfirsthalfofthearray.Repeattheprocessinthehalfofthedatathatshouldbeexaminednext.Stopwhenitemisfound,orwhenthereisnowhereelsetolookanditemhasnotbeenfound.5/28/2023数据结构与程序设计8TraceofBinarySearch
data[0][1][2][3][4][5][6][7][8][9]1526385762788491108119item=84
firstmiddlelastdata[0][1][2][3][4][5][6][7][8][9]1526385762788491108119
firstmiddlelastitem>data[middle] first=middle+1
item<data[middle] last=middle-15/28/2023数据结构与程序设计9Tracecontinued
data[0][1][2][3][4][5][6][7][8][9]1526385762788491108119item=84
first,last,middledata[0][1][2][3][4][5][6][7][8][9]1526385762788491108119
first,lastmiddleitem==data[middle] found=trueitem>data[middle] first=middle+15/28/2023数据结构与程序设计10AnotherBinarySearchTrace
data[0][1][2][3][4][5][6][7][8][9]1526385762788491108119item=45
firstmiddlelastdata[0][1][2][3][4][5][6][7][8][9]1526385762788491108119firstmiddlelast
item<data[middle] last=middle-1item>data[middle] first=middle+15/28/2023数据结构与程序设计11Tracecontinued
data[0][1][2][3][4][5][6][7][8][9]1526385762788491108119item=45
first,middle,lastdata[0][1][2][3][4][5][6][7][8][9]1526385762788491108119
first,lastmiddleitem<data[middle]
last=middle-1item>data[middle]
first=middle+15/28/2023数据结构与程序设计12Traceconcludes
data[0][1][2][3][4][5][6][7][8][9]1526385762788491108119item=45
lastfirst
first>last
found=false5/28/2023数据结构与程序设计13BinarySearchRecognizingEqualityP284Error_coderecursive_binary_2(constOrdered_list&the_list,constKey&target,intbottom,inttop,int&position){ Recorddata; if(bottom<=top){ intmid=(bottom+top)/2; the_list.retrieve(mid,data); if(data==target){ position=mid; returnsuccess; } elseif(data<target) returnrecursive_binary_2(the_list,target,mid+1,top,position); else returnrecursive_binary_2(the_list,target,bottom,mid-1,position); } elsereturnnot_present;}5/28/2023数据结构与程序设计14BinarySearchRecognizingEqualityError_codebinary_search_2(constOrdered_list&the_list,constKey&target,int&position)/*Post:IfaRecordinthelisthaskeyequaltotarget,thenpositionlocatesonesuchentryandacodeofsuccessisreturned.Otherwise,notpresentisreturnedandpositionisundefined.Uses:MethodsforclassesOrdered_listandRecord.*/{ Recorddata; intbottom=0,top=the_list.size()-1; while(bottom<=top){ position=(bottom+top)/2; the_list.retrieve(position,data); if(data==target)returnsuccess; if(data<target)bottom=position+1; elsetop=position-1; } returnnot_present;}5/28/2023数据结构与程序设计15BinarySearch--Mainvoidmain(){ Keytarget(5); Ordered_listmylist; for(inti=0;i<10;i++)mylist.insert(Record(i,10)); cout<<"Theorderedlistis:"<<endl; mylist.traverse(print); cout<<endl<<"Thetargetis:"<<target.the_key()<<endl; intbottom=0; inttop=mylist.size()-1; intposition=-1;
cout<<endl<<"Userecursive_binary_1Method:"<<endl; if(recursive_binary_1(mylist,target,bottom,top,position)==success) cout<<"Getthetargetinposition:"<<position<<endl; else cout<<"Targetnotpresent."<<endl;5/28/2023数据结构与程序设计16ResultTheorderedlistis:0123456789Thetargetis:5Userecursive_binary_1Method:Getthetargetinposition:55/28/2023数据结构与程序设计17BinarySearch--Main position=-1; cout<<endl<<"Usebinary_search_1Method:"<<endl; if(binary_search_1(mylist,target,position)==success) cout<<"Getthetargetinposition:"<<position<<endl; else cout<<"Targetnotpresent."<<endl;5/28/2023数据结构与程序设计18BinarySearch--Main
cout<<endl<<"Userecursive_bin
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025届吉林省长春市九台区第四中学物理高二下期末检测模拟试题含解析
- 2025年陕西师范大学附中物理高二第二学期期末联考试题含解析
- 云南省普洱市景东彝族自治县一中2025年物理高二下期末达标测试试题含解析
- 2025届河南省南阳一中高一物理第二学期期末考试试题含解析
- 2025届江苏南京市、盐城市物理高二第二学期期末统考试题含解析
- 客舱服务说课课件
- 2025届江西省浮梁一中高二物理第二学期期末统考模拟试题含解析
- 二零二五年度工业仓库场地租赁及配套设施合同
- 二零二五版企业班组安全生产合作协议范本
- 二零二五年度残疾人特殊教育机构服务协议
- DB37-T 5097-2021 山东省绿色建筑评价标准
- 儿科护理学(高职)全套教学课件
- 干眼门诊建设计划书
- 地下结构工程课件
- MBR膜系统清洗方案
- 中途测试、完井课件
- 实用美容技术高职PPT完整全套教学课件
- 戴维商务统计学第7版英文版教学课件BSFC7e-CH00
- 中国石油天然气集团公司管理人员违纪违规行为处分规定
- CJJ2-2020城市桥梁工程施工与质量验收标准
- 散户别跑:庄家洗盘模式全解析
评论
0/150
提交评论