版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、11/23/2021数据结构与程序设计 1Chapter 7 SEARCHINGnInformation retrieval is one of the important applications of computers.nWe are given a list of records, where each record is associated with one piece of information, which we shall call a key(关键字). (BOOK P269 FIGURE 7.1)n本章讨论顺序存储列表的查找操作,链接存储在第10章讨论。11/23/20
2、21数据结构与程序设计 2Chapter 7 SEARCHINGnWe are given one key, called the target, and are asked to search the list to find the record(s) (if any) whose key is the same as the target.nWe often ask how times one key is compared with another during a search. This gives us a good measure of the total amount of
3、work that the algorithm will do.11/23/2021数据结构与程序设计 3Chapter 7 SEARCHINGnThe searching problem falls naturally into two cases.nInternal searching(内部查找)(内部查找) means that all the records are kept in high-speed memory. nIn external searching(外部查找)(外部查找), most of the records are kept in disk files. nWe
4、study only internal searching.11/23/2021数据结构与程序设计 4Implementation of Key Classnclass Key nint key;npublic:nKey (int x = 0);nint the_key( ) const;n;nbool operator = (const Key &x, const Key &y);11/23/2021数据结构与程序设计 5Implementation of Key ClassnKey:Key(int x)nkey=x;nnint Key:the_key() constnret
5、urn key;nnbool operator = (const Key &x, const Key &y)nnreturn x.the_key( ) = y.the_key( );n11/23/2021数据结构与程序设计 6Implementation of Record Classnclass Recordnpublic:noperator Key( ); / implicit conversion from Record to Key .nRecord(int x=0, int y=0);nprivate:nint key;nint other;n;11/23/2021数
6、据结构与程序设计 7Implementation of Record ClassnRecord:Record(int x, int y)nkey=x;nother=y;nnRecord:operator Key( )nKey tmp(key);n return tmp;n11/23/2021数据结构与程序设计 8Test Mainnvoid main()nKey target(5);nRecord myrecord(5,9);nif (target=myrecord) coutyesendl;nelse coutnoendl;nn调用调用operator Key( )构造临时构造临时Key t
7、mp,采用采用void operator = (const Key &x, const Key &y)操作符比较。操作符比较。nOutput:nyes11/23/2021数据结构与程序设计 9Implementation of Search目录目录SeqSearch下例程下例程11/23/2021数据结构与程序设计 10Another MethodnRecorder中的成员中的成员operator Key( );主要主要完成从完成从Recorder对象到对象到Key对象的自动转对象的自动转换。可以用其他方法完成该功能。换。可以用其他方法完成该功能。nUse constructo
8、r to conversion from Record to Key .11/23/2021数据结构与程序设计 11Implementation of Key Classnclass Key nint key;npublic:nKey (int x = 0);nKey (const Record &r);nint the_key( ) const;n;nbool operator = (const Key &x, const Key &y);11/23/2021数据结构与程序设计 12Implementation of Key ClassnKey:Key(int x)n
9、key=x;nnKey:Key(const Record &r)nkey=r.the_key();nnint Key:the_key() constnreturn key;nnbool operator = (const Key &x, const Key &y)nnreturn x.the_key( ) = y.the_key( );n11/23/2021数据结构与程序设计 13Implementation of Record Classnclass Recordnpublic:nRecord(int x=0, int y=0);nint the_key() cons
10、t;nprivate:nint key;nint other;n;11/23/2021数据结构与程序设计 14Implementation of Record ClassnRecord:Record(int x, int y)nkey=x;nother=y;nnint Record:the_key() constnreturn key;n11/23/2021数据结构与程序设计 15Test Mainnvoid main()nKey target(5);nRecord myrecord(5,9);nif (target=myrecord) coutyesendl;nelse coutnoendl
11、;nn调用调用Key(const Record &r)构造临时构造临时Key tmp,采用采用void operator = (const Key &x, const Key &y)操作符比较后,调用析构函数释放操作符比较后,调用析构函数释放tmp。nOutput:nyes11/23/2021数据结构与程序设计 16Implementation of Search目录目录SeqSearch2下例程下例程11/23/2021数据结构与程序设计 17Sequential Search 顺序查找顺序查找 P272nError_code sequential_search(co
12、nst List &the_list,nconst Key &target, int &position)n/* Post: If an entry in the list has key equal to target , then return success and thenoutput parameter position locates such an entry within the list.nOtherwise return not_present and position becomes invalid. */nnint s = the_list.si
13、ze( );nfor (position = 0; position s; position+) n Record data;n the_list.retrieve(position, data);n if (target = data) return success;nnreturn not_present;n11/23/2021数据结构与程序设计 18AnalysisBOOK P273nThe number of comparisons of keys done in sequential search of a list of length n is:nUnsuccessful sear
14、ch: n comparisons.nSuccessful search, best case: 1 comparison.nSuccessful search, worst case: n comparisons.nSuccessful search, average case: (n + 1)/ 2 comparisons.11/23/2021数据结构与程序设计 19Binary SearchSequential search is easy to write and efficient for short lists, but a disaster for long ones.One o
15、f the best methods for a list with keys in order is binary search.首先讨论如何构建一个有序的列表。然后讨论针对顺序列表的二分查找。11/23/2021数据结构与程序设计 20Ordered Listsn有序列表的定义:nDEFINITION nAn ordered list is a list in which each entry contains a key, such that the keys are in order. That is, if entry i comes before entry j in the li
16、st, then the key of entry i is less than or equal to the key of entry j .11/23/2021数据结构与程序设计 21Ordered Listsnclass Ordered_list: public Listnpublic:nError_code insert(const Record &data);nError_code insert(int position, const Record &data);nError_code replace(int position, const Record &
17、data);n;11/23/2021数据结构与程序设计 22Ordered ListsnError_code Ordered_list : insert(const Record &data)n/* Post: If the Ordered_list is not full, the function succeeds: The Record data is inserted into the list, following the last entry of the list with a strictly lesser key (or in the first list posit
18、ion if no list element has a lesser key).nElse: the function fails with the diagnostic Error_code overflow. */11/23/2021数据结构与程序设计 23Ordered ListsnError_code Ordered_list : insert(const Record &data)n/* Post: If the Ordered_list is not full, the function succeeds: The Record data is inserted into
19、 the list, following the last entry of the list with a strictly lesser key (or in the first list position if no list element has a lesser key).nElse: the function fails with the diagnostic Error_code overflow. */nnint s = size( );nint position;nfor (position = 0; position s; position+) nRecord list_
20、data;nretrieve(position, list_data);nif (data list_data) break; /book p279 wrongnnreturn List : insert(position, data);n /调用父类的方法。调用父类的方法。n11/23/2021数据结构与程序设计 24Ordered ListsnError_code Ordered_list : insert(int position, const Record &data)n/* Post: If the Ordered list is not full, 0 = position
21、 0) nretrieve(position - 1, list_data);nif (data list_data)nreturn fail;nnif (position list_data)nreturn fail;nnreturn List : insert(position, data);n11/23/2021数据结构与程序设计 26Ordered ListsnError_code Ordered_list : replace(int position, const Record &data)nnif (position = count)return range_error;n
22、Record list_data;nif (position 0) nretrieve(position - 1, list_data);nif (data list_data)nreturn fail;nnif (position list_data)nreturn fail;nnentryposition = data;nreturn success;n11/23/2021数据结构与程序设计 27Ordered Listsnclass Recordnpublic:nRecord();nRecord(int x, int y=0);nint the_key() const;nprivate:
23、nint key;nint other;n;nbool operator (const Record &x, const Record &y);nbool operator (const Record &x, const Record &y);nostream & operator (const Record &x, const Record &y)nnreturn x.the_key( ) y.the_key( );nnbool operator (const Record &x, const Record &y)nnreturn x.the_key( ) y.the_key( );nnostream & operator (ostream &output, Record &x)nnoutputx.the_key();noutputendl;nreturn output;n11/23/2021数据结构与程序设计 30Ordered Lists-Mainntemplate nvoid print(List_entry &x)ncoutx;nnvoid main()nOrdered_list mylist;nfor(int i
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB 15208.4-2026微剂量X射线安全检查设备第4部分:人体安全检查设备
- DB32/T 5157-2025档案移交与接收规范
- T/HNNMIA 62-2023氢氧化铝晶种化学分析方法 草酸根的测定 离子色谱法
- T/CAAMTB 55.8-2021电动乘用车共享换电站建设规范 第8部分:液冷连接器技术要求
- T/CADERM 5018-2023核辐射外污染洗消车
- DB32/T 4753-2024文化场馆和旅游景区电子票务系统技术规范
- DB32/T 5243-2025农产品质量安全信用系统安全防护技术规范
- T/CAI 196-2023预制菜 陈皮虾制品
- T/CADERM 7009-2021压力测量型止血带
- T/CARM 024-2025三级综合医院康复医学科作业治疗室建设指南
- 2026年全民国防教育日大学主题团日:强国有我 青春有为课件
- IEC 62619 标准中文版文档(资深行业深度解读+资源指引)
- 高中品德素养测试题目及答案
- 广东电网有限责任公司交通安全管理实施细则
- 2026届招商蛇口校园招聘启动笔试历年备考题库附带答案详解
- 2026新教材语文 10故宫博物院 教学课件
- 人力资源管理绩效考核方案案例
- TSG08-2026《特种设备使用管理规则》全面解读课件
- 新成立业委会的工作计划书
- 2026中国新造船监理行业现状规模及前景动态预测报告
- DG-TJ 08-19-2023 园林绿化养护标准
评论
0/150
提交评论