数据结构与程序设计(19) 课件_第1页
数据结构与程序设计(19) 课件_第2页
数据结构与程序设计(19) 课件_第3页
数据结构与程序设计(19) 课件_第4页
数据结构与程序设计(19) 课件_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

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

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论