You are on page 1of 19
Dl + ply + Ly) tout pyfL, + Ly tect Ly) 7 : BB od as follows. : 4 wrem 2. Let L, and p, be as defined above. The arrangement in the table is optimal if and only if Ll Ae 7 hs Pip Pry > Pw Pyke shu 1 ») Sine g2G-s1 hol le ° By+B, We deduce from part a) if; Suppose that R, and R,,, are interchanged on the tape; the (ly ttlea +h) + Poa lyre bead 1 Poy (Lyphoct Ly + Lyas) + By (Ly toot Lys). With a difference of Bar $1+25)8, (-1) = 2P ppg -1 7 This theorem gives a closed form expressi expression for Pygy Which - : ic rally valted for paricular distibutions B and usu IY shows Pc Bast of the MF ~ rule is only a few percent shove Wy ~ Pusky, Trerefore if 7 <7,,, the given arrangement is not optimum. Tt also shows th i ors at Prerg 8 never more than twice the o follows that if the arrangement is optimal then (1) holds. Ip onversely, assume that (1) holds; we need to prove that the P= 3p for 1Si SN, the self - organizing table is always j nent is optimal, We know that any permutation of the records ted into the order R,R, ... Ry by using a sequence of inter- ‘adjacent records, Each of these interchanges replaces .. RR, RR, for some i 7 ly er than 5, when Vis reasonably large, and itis only abo = 1.386 times as many compar ined i optimum arangement a parisons as would be obtained in the § 3. Searching by comparison of keys cate ee ial mee Cieanizing method works even better than In this section we shall discuss search methods which are based mall groupes of keys tend to ccoe is bone ae not ind war ordering of the keys. After comparing the given argument K Tape searching with 8) (ef. D. Knuth), K, in the table, the search continues in three different ways, ig with unequal-length records. Suppose the table \ding on whether K < K, K=X, or K> K, With so many sorting methods at our disposal, we will have little in let p, be the jiculty rearranging a file into order so that it may be searched conve- ime of the search Ofcourse, if we only need to search the table once, itis faster to the above pendent (8 100 101 do a sequential search than to do a complete sort of the file; but ‘we need to make repeated searches in the same file, we are better o having it in order. Therefore in this section we shall concentrate methods which are appropriate for searching a table whose keys are order, K, < K, <... K,[R,, Ry... R, ate eliminated from consideration], Tn each of these three cases, substantial progress has been mad unless is neat one of the ends of the table; this is why the ordering lea to an efficient algorithm, | Let S= {K, < K, <... < K,) be stored in aay K[I.., KqiJ- K, and let a U, In order to'decide a € S, we compare a wi some table element and then proceed with either the lower or the upperp of the table, idea leads to the following general algorithm from whi Various algorithms can be obtained by replacing lines (2) and (7) b specific strategies for choosing next: (1) low © 1; high — (2) next — an integer in (low, high]; (3) while a # K [next] and high > low (4) doifa and Sc (0,a), Then - deteriorates to linear search. Iways and interpolation search case behaviour is much better. We shall rewrite the binary search follows: i ds R,,R, rithm B (Binary search). Given a able of records R,, eys are in increasing order K, < K, <... K,, go to §; andif K~ K,, thealgorithm terminates successfully. 4, Set ui - 1 and return to 2 §, Set? i+ 1 and retum to 2 ; Note that if w K, the right subtree is used, An unsuccessful search A. In this case the root of the lead to one of the extemal” square nodes numbered 0 through Nj ve T corresponding to Algo- r example, we reach node [6 Jif and only if K, < K-< Ky. Nat : The binaty tree corresponding to a binary search on Nrecords tia labelip #1 |), be constructed as follows: 2 tb exectl IEN= 0, the tree is simply 0]. Otherwise the root node is ‘btree 7, and the right subtree 7, contain each exactly p the left subtree is the corresp: ince and the righ it 2s2pt+i<2 counted only intemal vertices corresponding to a successful search, Tn an analogous fashion, any algorithm for searching an ord table of length V by means of nonredundant comparisons can be rep hat p sented as a binary tree in which the internal vertices are labelled with © Qh - =, , hence p < 24, Because 2! < p <2! v->, numbers 1 to N and extemal vertices with 0 to N. Conversely, any binary tree comesponds to a valid method Searching an ordered table; we simply label the vertices [5] ® wn hypothesis for both T, and 7): A(Z)) 22-1 ence p 22M, since pis BaN-5, i iberof key - of Tbeing equal to the maximum num ty tors entoened by Alnor B fora succesful seach, Tf an integer and also * we can apply the = A(T) = k, hence @ BJ. WG Ge] in symmetric ‘der, from Teft 101i 01 pana vertices of T, and T. are on level k hence if ifthe tree has NV intemal vertices and consequently, V+ 1 external on Peat Ifthe search argument input to Algorithm B is K,,, the algorith y 281, which implies that 2'< N'<2** il terminal vertices of T are on level k + 1. 1 since N=2p+1 makes comparisons K > K,, K

You might also like