版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Searchi ng9Pla n to give the lecture by clear expla nati on with particular examples.Make use of all kinds of in strume nts especially multimedia.To attract the atte nti on of stude nts through putt ing questi ons and other in teractive method.Try to lead the stude nts their way of thi nking.Course
2、Layout1. import2.newcontent3. summaryIn troduce the new course1. sequential search and three Variations2. binary search1. review the key points and the difficulties2. questions from students3. homework3 mi nu tes42 minu tes43 minu tes2 mi nuteContentOne of the most com mon and time-c onsuming operat
3、io ns in computer scie nce is search ing, the process used to find the location of a target among a list of objects. In this chapter we study several search ing algorithms. We beg in with list search ing and a discussi on of the two basic search algorithms, the sequential search-including three inte
4、resting variations-and the binary search.After review ing the con cepts of list searches, we discuss hashed list search ing, in which the data key is algorithmically manipulated to calculate the location of the data in the list. An integral part of virtually all hashed list algorithms is collisi on
5、resoluti on, which we discuss in the last sect ion.Although we discuss the list search algorithms using an array structure, the same con cepts can be found in linked list searches. The sequential search, along with the ordered list variation, is most com monly used to locate data in a lin ked list.
6、The binary search tree is actually a structure built to provide the efficie ncy of the binary search of a tree structure. These searches are coveredin their respective chapters.2-1 LIST SEARCHESThe algorithm used to search a list depends to a large extent on the structure of the list. In this sectio
7、n, we study searches that work with arrays. The two basic searches for arrays are the sequential search and the binary search. The sequential search can be used to locate an item in any array. The binary search, on the other hand, requires an ordered list. The basic search concept is shown in Figure
8、 2-1.Sequential SearchThe sequential search is used whenever the list is not ordered. Generally, you will use the technique only for small lists or lists that are not searched often. In other cases you should first sort the list and then search it using the binary search, discussed later.In the sequ
9、ential search, we start searching for the target at the beginning of the list and continue until we find the target or we are sure that it is not in the list. This approach gives us two possibilities: either we find it or we reach the end of the list. In Figure 2-2 we trace the steps to find the val
10、ue 14. We first check the data at index 0, then 1, and then 2 before finding the 14 in the fourth element (index 3).But what if the target were not in the list? In that case we would have to examine each element until we reach the end of the list. Figure 2-3 traces the search for a target of 72. At
11、the end of the list, we discover that the target does not exist.Sequential Search AlgorithmThe sequential search algorithm needs to tell the calling algorithm two things: First, did it find the data it was look ing for? And sec on d, if it did, at what in dex are the target data found? To an swerthe
12、se questions, the search algorithm requires fourparameters: (I) the list we are searching, (2) anin dex to the last eleme nt in the list, (3) the target,Location Wanted4213614629182278177叩a|1J a|日3 4|4刑5 m岡 a7 a0| afl| 10 a|11|and (4) the address where the found elementindex 072 not equal 斗index4al2
13、 a3 a4 a&L a同 apl 0 49)146291g22781nT-argel 72al 1J10斗211462?1822717710珂订总2由曲自5J e岡班刃缸內剧刖现10班4361462918227S17710忒D:时1卡制旬扯引S制轨创疏7 &阖詛射那姐亦173 nol aqfual 10ind&x崔1刮囚刮3酊斗日5引右|日引乱创日1421se1491S227S17711Note: Not oil 1st paints aro shown.index location is to be stored. To tell the callingalgorithm whether
14、the data were located, wereturn a Boolean-true for we found it or false for we didn t find it.Although we could write a sequential search algorithm without passing the index to the last eleme nt if we did so the search would have to know how many eleme nts are in the list. To make the function as fl
15、exible as possible, we pass the index of the last data value in the array. Gen eraliz ing the algorithm by pass ing the in dex to the last item is also a good structured desig n technique. With this information, we are now ready to code Algorithm 2-1.Variations on Sequential SearchesThree useful var
16、iations on the sequential search algorithm are (1) the sentinel search, (2) the probability search, and (3) the ordered list search. We look at each briefly in the followi ng sect ions.Sentinel SearchIf you examine the search algorithm carefully, you will note that the loop tests two conditions, end
17、 of list and target not found- Knuth states that When the inner loop of a program tests two or more conditions, an attempt should be made to reduce it to just one condition. Ifwe know that the target will be found in the list, we can eliminate the test for the end of the list. The only way to en sur
18、e that a target is actually in the list is to put it there yourself. A target is put in the list by adding an extra element (sentinel entry) at the end of the array and placing the target in the sentinel. We can then optimize the loop and determine after the loop completes whether we found actual da
19、ta or the sentinel. The obvious disadvantage is that the rest of the processing must be careful to never look at the sentinel element at the end of the list. The pseudocode for the sentinel search is show n in Algorithm 2-2.Probability SearchOne of the more useful variations is known as a probabilit
20、y search. In the probability search, the array is ordered with the most probable search elements at the beginning of the array and the leastprobable at the en d. It is especially useful whe n relatively few eleme nts are the targets for most of the searches. To ensure that the probability ordering i
21、s correct over time, in each search we exchange the located element with the element immediately before it in the array. A typical implementation of the probability search is shown in Algorithm 2-3.Ordered List SearchAlthough we gen erally recomme nd a bi nary search whe n search ing a list ordered
22、on the key (target). If the list is small it may be more efficient to use a sequential search. When searching an ordered list sequentially, however, it is not necessary to search to the end of the list to determine that the target is not in the list. We can stop when the target becomes less than or
23、equal to the curre nt eleme nt we are testi ng. In additi on, we can in corporate the sen ti nel con cept by bypass ing the search 1oop whe n the target is greater tha n the last item .In other words, whe n the target is less tha n or equal to the last eleme nt, the last eleme nt becomes a sen tin e
24、l, allowi ng us to elimi nate the test for the end of the list.Although it can be used with an array, the ordered list search is more commonly used when searchi ng a lin ked list. The pseudocode for search ing an ordered array is found In Algorithm 2-4.Binary SearchThe sequential search algorithm is
25、 very slow. If we have an array of 1000 elements, we must do 1000 comparisons in the worst case. If the array is not sorted, the sequential search is the only solution. However, if the array is sorted, we can use a more efficient algorithm called the binary search. Gen erally speak ing, we should us
26、e a binary search whe never the list starts to become large. Although the defi niti on of large is vague, we suggest that you con sider binary searches whe never the list contains more than 16 elements.The binary search starts by testing the data in the element at the middle of the array to determin
27、e if the target is in the first or second half of the list. If it is in the first half, we do not need to check the second half. If it is in the second half, we do not need to test the first half. In other words, we elimi nate half the list from further con siderati on. We repeat this process un til
28、 we find the target or determ ine that it is not in the list.To find the middle of the list, we need three variables, one to identify the beginning of the list, one to identify the middle of the list, and one to identify the end of the list. We analyze two cases here: the target is in the list and t
29、he target is not in the list.Target FoundFigure 2-4 shows how we find 22 in a sorted array. We descriptively call our three indexes first, mid, and last. Given first as 0 and last as 11, we can calculate mid as follows:firstmid0511Imid = (first + last)/2Because the index mid Is an integer, the resul
30、t will be the in tegral value of the quotie nt; that Is, the result Is truncated rather than rounded. Give n the data in Figure 2-4, mid becomes 5 as a result of the first calculati on.mid =(0 + 11)/2 = 11/2 = 5At in dex locati on 5, we discover that the target47甘101421223662778191日0a11且圍日冏 apt切5|启冏
31、且7|咸丙注阳aW刖仆、firstmidlast681122 21478101421223662778191日m1且圍司3 mH b5冷问且目冏冷9纽22 v 247a101421223662778191酊0制11 3罔自割 3(5列创胡7自囘酣 a 01吕111ESHfunction lemninatesftrsl mid last$22 equals 22is greater tha n the list value (22 21). We can therefore elim in ate the array locati ons 0 through 5. (Note that mid
32、is automatically eliminated.) To narrow our search, we assign mid + 1 to first and repeat the search.The next loop calculates mid with the new value for first and determines that the midpoint is now 8.mid = (6 + 11)/2 =17/2 = 8When we test the target to the value at mid a second time, we discover th
33、at the target is less than the list value (22 62). This time we adjust the end of the list by setting last to mid-1 and recalculate mid. This step effectively eliminates elements 8 through 11 from consideration. We have now arrived at In dex locati on 6, whose value matches our target. This stops th
34、e search. (See Figure 2-4.)Target Not FoundA more in terest ing case is whe n the target is not in the list. We must con struct our search algorithm so that it stops when we have checked all possible locations. This is done in the binary search by testing for first and last crossing; that is, we are
35、 done when first becomes greater than last. We therefore see that two con diti ons termi nate the bi nary search algorithm: The target is found or first becomes larger tha n last.Let s dem on strate this situati on with an example. Imagine we want to find 11 in our binary search array (see Figure 2-
36、5). In this example, the loop continues to narrow the range as we saw in the successful search un til we are exam ining the data at index locations 3 and 4. These settings of first and last set the mid in dex to 3.mid = (3 + 4)/2 = 7/2= 3 The test at in dex locati on 3 in dicates that the target is
37、greater than the list value, so we set first to mid + 1, or 4. We now test thedata at locati on 4 anddiscover that 11 -6 133L1781D1421223677B19117/at aHa(:3刑4 a5| e圍 a(7臼8j ajg a(1Dfirst mid test斗7a1014212236628191现0现T| *|2罩I4日冈训创训7训引测卅W現11fingt mid 愉別HSEFumctjon- lerminetesAnalyzing Search Algorith
38、msOf the five search algorithms discussed, which is the best? An applicati on often determ ines which algorithm should be used, but we an alyze the algorithms to determ ine which is most efficie nt.Sequential SearchThe basic loop for the seque ntial search is show n below.while (looker last & target
39、 != Iistlooker) looker+;This is a classic example of a linear algorithm. In fact, in some of the literature, this search is known as a linear search. Because the algorithm is linear, its efficiency is 0(n).The efficie ncy of the seque ntial search is 0(n). |The search efficiency for the sentinel sea
40、rch is basically the same as for the sequential search. Although the sentinel search saves a few in struct ions in the loop, its desig n is ide ntical. Therefore, it is also an 0(n) search. Likewise, the efficie ncy of the ordered list search is also 0(n) .If we know the probability of a search bein
41、g successful, we can con struct a more accurate formula for searchi ng an ordered list. This improved accuracy, however, turns out to be a coefficie nt in the formula, which as you will recall is dropped whe n using big0 an alysis.It is not possible to generalize the efficiency of the probability se
42、arch without knowing the probability of each element in the list, On the other hand, if the probability of the first few elements of a list total more than 90%, it can be a very efficient search, even considering the additional overhead required to maintain the list. In general, however, we recommend the binary s
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年商务领域安全生产检查计划
- 客房主管工作计划
- 人教版实验教材五年级下册语文的教学计划
- 交通局的工作计划
- 2024年乡镇计划生育协会工作计划开头语
- 幼儿园大班保育计划024 幼儿园大班保育工作计划
- 秋季语文教研组工作计划语文教研组工作计划
- 2024年电工工作计划
- 2024年乡镇工作计划开头语
- 广安市岳池县2023年八年级下学期《数学》期中试题与参考答案
- 医学影像学论文5000
- 地下泉眼封堵施工方案
- 口腔诊所医师技术操作规范流程
- 人教版小学语文二年级上册期末试卷
- 二年级数学上册解决问题专项复习课件
- 简易租房合同下载word
- 国开电大-工程数学(本)-工程数学第4次作业-形考答案
- 共点力的平衡条件
- 腹腔镜肝切除手术配合
- 高中数学-2.1.1 数列教学设计学情分析教材分析课后反思
- 2023琅琊榜入榜统一考试(江左卷)语文试题及答案
评论
0/150
提交评论