數(shù)據(jù)結(jié)構(gòu)lecture17searching1_第1頁
數(shù)據(jù)結(jié)構(gòu)lecture17searching1_第2頁
數(shù)據(jù)結(jié)構(gòu)lecture17searching1_第3頁
數(shù)據(jù)結(jié)構(gòu)lecture17searching1_第4頁
數(shù)據(jù)結(jié)構(gòu)lecture17searching1_第5頁
已閱讀5頁,還剩4頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

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岡班刃缸內(nèi)劇刖現(xiàn)10班4361462918227S17710忒D:時1卡制旬扯引S制軌創(chuàng)疏7 &闔詛射那姐亦173 nol aqfual 10ind&x崔1刮囚刮3酊斗日5引右|日引亂創(chuàng)日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列創(chuàng)胡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現(xiàn)0現(xiàn)T| *|2罩I4日岡訓(xùn)創(chuàng)訓(xùn)7訓(xùn)引測卅W現(xiàn)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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論