Welcome to Scribd!
Academic Documents
Professional Documents
Culture Documents
Hobbies & Crafts Documents
Personal Growth Documents
Unit 3 (Searching and Sorting) PDF
fink con either be sigifmosk of element” of the aven anay. > Sdeck medion as the pi ele ie Loop -T u Ee of (ACO 8 < 0am Le icodeh BY loep 9 pee ye (NSSep PS Qucene ort (A 1916 » Ex Quick soot eee Se , —— ary= Fossselela co] 70 t i a ; a ng pival = spent ; re 56) feo. 9<6,unD Le *] id Fohalrae. rele eh me. deme fea i: 4 repeal of e i‘ Fioverne. laments from Jz bow to heh es J 0 $= a ~ Pos \ age’ men |=) wap noyeacy ) Vielen Since £0 E10 wit \ i= 0 weo Sime 30 < Pou ‘a Jel since as) [Ss a : me ames | Jeu cinco gaa ee Swap Co SineRAdrx SORT ] Bachan cl One Siting alembhm Par i o . USER he Aconicce te ef Mlbing names in_abphabHica) | » Whe we have aise = the taclix 12 26 Co 26 burch) heewiuse Dilbert a éughish ale eis Seas alles pipe Ged Lae oo Sealing Jo Joab Syntificant ig & No Comperison kelweon olaty is needed) The concoph of radix Sort ig ts do digi 7 aero 120 4s do ay Sertusy 5 902. Fe se 62Kerk 34.9 O54 904, 123, 569,892, ss5 308,91) UL Numbs_ Sa 91) 654 SY4s— | | | — ae ss | z| Bi so) 308 Numb | o 1 Bos AU] 23 123 | ; aay Sys Se a Sos | Sér qart He aire tape 14 wha penal 36 {bucket is sorted veal - [ngcl I aah ee fines 0) a ee As ey had. a staning Hs _ Kolbe w wm Be Binw no: of ae Merz. eet mia means more tire wheres nl Aolucion vthsh cakes ti tke. Fa _deahah» Ls Hist Af searching, in time prsports ep! clara te tast oer toate Lape det unciast mol fost solution unt the kok fin a small om nizaicon whee numiacr gf loo. Each er has Uenag ire Enea kline the emp cod _In_an ae ack, ar an mdex mip @ Lhe o p thou bn fe gene 1+ ke resi of heya ox fo) Crm Ree cord ae Vital Emp 3 Tos Reads “op Tee é ee assume thal dame en In cthis caampl i JiweiaieThis mexly a wes i & storage ‘ { Sspp on ite we jus ae ae 2 each sin Vrper. For ena, will Lee element ane this fo Covert 4 S-degch = Value of + So heap g_mechanssm Inte _'2-degct army taden = ee iule’ -heed @feanchion ushich os © xe —Hash tyble and i ey ee aks 1A ea waa cote nde caboose a sh a iad ero go qenrake watnek of the kay C2. digit a» ba gyn ns vie da , Ioneee eo Se ] (SG 2 kegs (rep Pamk Botha same Loeabion: the Te a AA Calliseicm. s ate ae e ©) Dao hash tite On ebemenf- uth sry k is yndict htc) qoak k= Et meant a hash panchin $h) is a at 1 red te calelale the Gadi ak which the clement unt by Wie ust her stead Hash A fen chim la A A fear lion th @ Alffimatocal formule | ase, Q Key, Rroduccs om Prbeg sn Which Ca was _an fedex Gor Me keg 9 the hath able a4 gad, hash fenchon cae Pe Less dumtoa 1 ————_ 1 Dayation Patel ee t we ae aa M a pnme Se firs vo la ye betel 7p pees Gi) | Absyays aud dome veloues of m fe e- m should nit be Jao! power FE m_,Atince_of m2 oP thon h(x) 5 just | Lowest ay bs of &. Porm oioels_ of Ip sheulJ be anpold ¢ the appleeskeons coals withideerwal Aumos Oley ley 2 ah etant mdhad > Squ07e Hh =e ae ee nO) = Loe 3 hire Kis the. Keen ex Seppe sth hash table he Loo momen, eg Eas ies S Ky rk, > 124+34rS~ MulErpli cadcon Metived = flan loo (234s¥003: flaw ( Ie (4419-53 = fe ¢ Zi3 & Collision Resolution Techniques Hashing in data structure falls into ac the’ same index number in the hash problem because each index in a hash table is supposed to store only one value. Hashing in data structure uses several collision resolution techniques to manage the performance of a hash table, {tis a process of finding an alternate location. The collision resolution techniques can be named as- 1. Open Hashing (Separate Chaining) 2. Closed Hashing (Open Addressing) + Linear Probing + Quadratic Probing + Double Hashing 1. Open Hashing (Separate chaining Collisions are resolved using a list of elements to store objects wi key together. ollision if two keys are assigned table. The collision creates a Suppose you wish to store a set of numbers = {0,1,2,4,5, table of size 5. Now, assume that we have a hash function H, such that * So. if we were to map the given data with the given get the corresponding values H(0)-> 0%5 = 0 H(1)> 1%5=1 H(2}> 2565 =2 H(4)-> 4%5 = 4 H(5}> 5%5 = 0. H(7)-> 7965 =2 + Clearly 0 and 5, as well as 2 and 7 will have t] this case we'll simply append the colliding va by their hash keys,e can be significantly large and the hash mplex, also the data being hashed would be put the idea remains the same. shing but it has its own’ demerits. i Obviously in practice the table siz function can be even more co G more complex and non-primitive, This is an easy way to implement ha: ‘The lookups /inserts/updates can become linear [O(N)] ins constant time [0(1)] ifthe hash function has too many | Itdoesn't account for any empty slots which can be leverag efficient storage and lookups. e a good hash function to guarantee eve + Ideally we requir of the values. Say, for a load factor xemumberyortebsects: stored! inl table/size © >i) a good hash function would guarantee tia associated with each key is close to the -der in which the data is sto Note that the or structures) is based upon the implementa include insertion order, frequency of acce 2. Closed Hashing | ‘This collision resolution technique size, During insertion, if a col bucket is foul until an emp s fou(*) R- linear Probing b. Quadratic probing ©. Double hashing a. Linear Probing ‘The idea of linear probing is simple, we take a fixed sized hash table and every time we face a hash collision we linearly traverse the table in a cyclic manner to find the next empty slc Assume a scenario where we intend to store the following set of numbers = {0,1,2,4,5,7) into a hash table of size 5 with the help of the following hash function H, such that a(x) = x45. * So, ifwe were to map the given data with the given hash function we'll get the corresponding values H(0)->0%5 = 0 H()>1%5=1 7 H(2)> 25 =2 H(4)> 4965 =4 H(S)> 5%5 =0 in this case we see a collision of two terms (0 & 5). In this situation we ~~ movVe linearly down the table to find the first empty slot. Note that this inear traversal is cyclic in nature, element during the search we star inal key is reached | 0 | 0 | ie. in the event we exhaust the last ‘again from the beginning until th HOe = (HCD) modi ite ef hash table: i - Woo = Ce med va), thak varies from 0 + In this case our hash function can be con: HOR, i) = (Wim) + 4) 8%) where N is the size of the table and i repre variable which starts from 1 m1 Despite being easy to compute, i performance, this suffers froo oy 3 2 on mere. He fatlirg the Same prbnay ie ) Sequence elem ts Get grouped together, which eventually reduces the efficiency of finding elements or empty buckets). b. Quadratic Probing This method lies in the middle of great cache performance and the problem of clustering. The general idea remains the same, the only difference is that we look at the Q(i) increment at each iteration when looking for an empty bucket, Where Q(i) is some quadratic expression of i, A simple expression of Q would be Q(i) = i%2, in which case the hash function looks something like this: 4 B(x, i) = (H(x) + i*2)3N ie + Ingeneral, n(x, i) = (H(x) + ((c1\#i42 + c2\*i + ¢3)))8N, for some choice of constants c1, c2, and c3 + Despite resolving the problem of clustering significantly it may be the case that in some situations this technique does not find any available _ bucket, unlike linear probing which always finds an empty bucket, + Luckily, we can get good results from quadratic probing with the combination of probing function and hash table size which will guarantee that we will visit as many slots in the table as possible. I particular, if the hash table's size is a prime number and the probi function is (x, i) = i*2, then at least 50% of the slots in the tab! be visited. Thus, if the table is less than half full, we can be certain free slot will eventually be found. Alternatively, ifthe hash table size is a power of two and the p function is (x, i) = (i*2 + i) /2, then every slot in the table visited by the probing function, i + Assume a scenario where we intend to store the followin, numbers =,(0,1,2,5} into a hash table of size 5 with the k following hash function H, such that #(x, i)——~ Ug Clearly 5 and 0 will face a collision, in which case we'll do the following: - we look at 5945 = 0 (collision) - we look at (5%5 + 142)%5 = 1 (collision) - we look at (5%5 + 2%2)%5 = 4 (empty -> place element here) c. Double Hashing This method is based upon the idea that in the event of a collisio another hashing function with the key valueas an input to find wh open addressing scheme the data should actually be placed 2t._ n we use an in the Inthis case we use two hashing functions, such that the final hashing function looks like: A(x, i) = (HL(x) + i*H2(x)) 89M ‘Typically for ai (x) = xtwp good H2 is H2 (x) = P ~ (x8), where P is prime number smaller than PF) h never evaluates,to zero and ensures tha + Agood H2 is a function whic all the cellg of a table are effectively traversed «Assume a scenario where we intend to store the following set of numbers = {0,1,2,5} into a hash table of size 5 with the help of the following hash function H, such that H(x, i) = (HL (x) + i*H2(x)) a5 ni (x) = x85andH2(x) = P - (x$P), where P=3 (Bisa prime smaller than 5) Hyon = 5 782° yy) 3-573 axl - we look at 5%5 = 0 (collision) - we look at (5%5 + 1*(3 - (5%3)))%5 = 1 (coll = we look at (595 + 2*(3 - (5%3)))%5 = 2 (ce - we look at (5965 + 3%(3 - (5%3)))%5 = 3 (empty >