Вы находитесь на странице: 1из 28
z See Arey eee Slat} Ata] Riabt ; L AYa]+ aT2y =4 2,-,4,8 Genie a 4 | Cenopave ATAI 4 le i j B>4. Shift ATs? Right AvaD=ataa-a | fe Jaeo ® Ceopsne ATO] ¢ k. | i-@ Bt .. De Noling . [AUje T= 4fa]=k- 4. |-w,4,3,4,5 Hewe, to We Te pas, we select Se elercest ATi and elenreslé 8 be left of ATT jo We army are Staflet) one yorittes |b We rigibt wot) uw fict aco cleo Whics b lees Mao ATC]: thes, We elercost ATI is Paced So We ammay, next 16 l15e Ander elensest found above Cuting while Loop io 15, Ager tno.) Hobe tosterd Of cludting elersealé te Use atgied www.itgeeks.in ick soRT precedure eoRt Cvay 79,19 tintegay ) vay 0,9 ,m: ioteger ; begiw td Cron) Meo begin or Jrznt; Kee Aro; { Were, pivoh te bY nepect vepead Ceete) | wale Ck > ATI); “Repeat SON while Ck < AT§ZID: ff Cr Cin vi SEAS. lege (Gane NS ast and’ al clensedlé Ls “il6 ovgot P CAT II, , AToID great bars Ie pivod- ie, We Usk Array 6 partifesed il Q- Tee Iwo parhhons (At), ~ p ATGZ-J ana AVF4i7,---,ATHID cao be sorter - Wedlepesctertly lay calling E083 © Wena @soRT Cro, j-1) 3 [kaon 5 use j-1) @sornt Cj+i,o)> { | Cres jer ¢ uaerJ Bg Besuscce a | elensenls is Value» | ac & a7] 4 64 24 [sais [ae] ss | Moder > AB OY Ss PEG Al We but cau ef a@sons = sors (4,10) WM=4 f¢ =ld- ATI= 26, 34) toenesect & +i Pind ao elenseot > pivot. (- ne? Wem, 27> a6 2 £2 2. Ctoder valu y 22) A) Decvenseotk Ff fut trad a elensect < pivet Cae? as 7 oa r) ww tleeas. 7 at do tevebange AVC) 4 AVSI ie, Ata) ¢ 4h] > 344 1%. > wisi cs 26,5,19,1, 61,1, 54,15, 48,34 AD Repeat ctep 4 above - Wew, et>ae. -. (2S [. ated: er) 5) Repeat Step 2 above. Wew, 164 a6 Ls fre Uv atea- te) 6) Again Pej Gece SKe “+ Swap ATS) and AT?J. fe, ar gis D> Lied is 26,5,19,1, 15,1, 59,61, 40,39 4) Repest step 2 - 64> nem fe F €) Repeal step a W< ae jz6 A) dea, 2 cweses J (ie, 76) wm Loj- Totevelrange pivot CAT ro) ) and ATII www itoeeks.in Zp, adte Wes ORORG, We array | dst & fs4.5 19 1 18] ae fa a Ae 34] The Status of We Uist at endo Susece quest cau 16 Bsoni is gives Lafow. AP Ibe end , We Wave Ibe army Of Derrests ito swted order es | mon| & & % Ay Re fe Re hq Bio a | 2 os; sf u fie wl] ae fea 6 ge ad i 2) 4 2 2 sou fia wl ae fe 4: 4e 23] 4 i 4 5 : Bt 5 ui 1 26 Gy oe 4% Bal | Se wl a 9s i ie i ae [ae aq so [er] ' i ¢ | + e a Ss u is 14 a6 az 4e sa [el] e to wo 2 a u ea a) a6 3H 4e S44 al Note: Fine conszlerity of Sutcktert + Aveyage > © Cntogn). Wore| cave = 0 Cr*) Worst case consplexily cecum when IB. Demecls iro We army is incHally surted io decreating ordey ee ae ING: « = ; TSAI WEES. 7 IZLe SORT * Procedure Bumert (A: ary [4-0] of elesesttype) ; Vay U,j + teteger; begio Ga Gaertn ine iets For j= n dewold fer ato tf Caty-iJ > ats) Wer Swap CATj-17, PTI); Proceterme goer (vay 2,92 clersecttype ) y Vay tenop : tener Hy pe y begie devop t= mieys Yrs denop ; end let Away A be We amet as Wen EE Sut 4 |a}| 2 Nadues OS a te ates 5 ATsI+ 4 5 ATES atay=S 5 Ateiet : Tanthally J Buble swt will gosh Me above array ay = ctor at gives below : Pass | & | J Acties per frrsed Anvay [Lick Trttal emodites'(n-5) | 2,5,4,1,2> a {5 j-ie4- t Comapare AAT ¢ OTS) ayaa #AtTSI (14a) | Do NolBing- ENE 4 | fete 3- ATI ¢ © TH) a>a (Atsd> ate) | \ Apleterange [Senp AF 4! 3,5,4,4,2 Ee} ja a. | compare atay ¢otad i | i | Soa | i Eye Ane an traake in Aetieo Pertoresad Arvey tisk Conspare ATi) ¢ etal ard Susp 2¢4 4,3,5,4,2- Jes a, |Cospare 414] ¢ e187 4,2,5,2,4 | [eeopare ATO] 4 ATHY } 1 | isa | | dolerdoang< S42, 1,3,2,5,4 L3 if-1- 2. | | cazrenetPa rer fATed [ara i be Toleebacge 243. 4,2,3,5,4 i HS is es | ! | lena AI ¢ 4107 | ie || 5>4 ! | i '. Swap 5 4 4 11,2,3,4,5 | | ih jict- 3- ' 3 Conspare AT 23 ¢ ATH Actign, tattered | Array 5 ity ve Nee _ | canopare ATHI 4 ATSI : ; es | L. Re NolBieg 1,2,2,4 5 Huot: ene, adter wee ath part, we bave We smalleas value in tee aSt posites Abbey 16. Second paw, Oe ave Ie Spates] a Peres’ in te, Fiat 2 pohors (Be avvay. Atte we 116 pas, ve ave We gmaltest & derels io We Rut f potitinss io We amy fd & (on ) Procedure Busenre (ver A: Avvay [4.07 °F elerseattype) New Py + wteget: begins do Crea B D1 de dor yi= 16 i do 4 CATY 1 > Atsey) ee swap CAT SH, ATSID: end ; Usk: Were, afley ise TH pats, we bave We Rorgest & ee eel www topes: in Array A wale elerseclé - ae aa Pass is j Akos pevfoiced Array fist t Trial ceoctiteo (n=4)] 3,5,4,+% ! | a a a jue : Cospare ats] 4 ARI ' yes. p Men 2,5,4,% ; D2 Jtte2- : i Coropame Alal ¢ ATs] lexan. i See eye 3 irre 4 | coo pave ATal ¢ avy | | 2,4,4,5 t i 2 a Galea. Oi [Compare ati] ¢ Tad 2D lars | Arreg 2,2,4,5 ss a pte 4-3 Comopare ATG ATAI =a | oe joes I: Suap 2¢4 © SELECTION SORT > SELSORT (Vay A: Amay Tt Procedure. ae dowiodex + ioteger ; Agee ioteger ; min» demestiype ; Legire tor Ors 4 nt de be gio mint = ATID: Jorioder 2 £5 ee A OW 4 ( ' min > ATS] ) Mee be giv woinrs ATI: deta eelenlt ai =A) of slersect) j Wu TCeens. 1 end = Swap CATT, AL Qnoieder) ) ; end Pase Activo Deray Suite) coodition- 2,5,4,¢ *o GB a - a muts ATIT= 2 A Lovindex .= 4 Begio 2 | Conopare mio ~ AT SI te, 34 ata] BPS. Do nolbing Be A, al i = a 3 Cemopare min ¢ AT2] i 2H. Be nelBing | jp. ees 4 Conspane mis ¢ ATHI bara le - po Mme ATH = 2 | Rowiadex = 4 i ce | Av4d 1 | Swap min ¢ 4Ti17 I j fe, ago 41,5,4,3 i Mine Alay. S ie | i + j Levoindex > 2+ Aetioo GIRS af v E te A plier ff, i we iy is. Lmsirdex > 2 1 | A | Comapare mato $ ATAI | | Are | ' (Ee irapes | | | Amdiodex = 4 | i i. |= ate] Ata) | | Swap min ¢ 4707 | | 1 1 i | | te, 245 A Sy aoe 2 2 mins AT27=4 Lwdiodex = 2 Regio _— 4 Comopare min + AY4) AFS-. Do neliding fd Swap Aa) 4 Ata) 4 ,3,4,5 mir Avid | bey 4 gh Herve, in We 18 pats, we Sedect We Lmoest efercents | ot of AlPT, ALENT), 41h) and Wes sump UF wit AVIV. ie, ig te BRA pan, mineuns out gy Alade~ Al & fou at 4 U & swapped wild Aa]. & @ rian tne n Bia vy peaks NS - We aray aud So o0- PER TION SORT ProceRure INsoRT (Vey A: Array Ta-12J 9g Percectiype ) Var a5: integer”; ot elersecttype > begin ATo} += — 9999. Cor -a) dos 2 bn do begin SEER C= 3) Kee ATed; blule Catp] >k) de begie AUj+43 = ATGI; es » S t ® end end ; d Actiso ” perey c Liv AS. . ‘ ESS cake Bey AW, = is 4 | Ke Atay=5 Conspeme ata} ph 1 | 2758. Do nobbieg. [ACgu]> Atal =k ie, ATay=-& 2 |k- atad= 4: ' 1 I | | Coopare Atal ¢h | . S24. Sheth ATA Right) 1 1 3 i. AYaId= Atal ar (f+ j-r- 2 \ 2 | eonpare AYal ¢ k- | | i 2#4. Bo Nolbing. jATinI = ATad=kea, 2,4,5,4 | | i ! i Ke Ate yea | Comopa mre Atal ¢ ks. 1 Boa, Sit Ale) Right \ jAUjed+ ateI~ 5 34,5 i i | ee f is 7 7 | ie ae ee ERY PENS Asrey A> 4. lott afay Riglet > AYa]~ 4T2) =4 3,-,4 6 Cerspave ATI ¢ le B> 4. Shift ATT Right Afals ala} =3 | | {AUfi]= 4fad=k- 4. |-w, 4a 3 4,5 Hewe, io Wee Tle pas, we Select Be elensest ATi Gnd elenresls 6 Ie led) of ATIT to: Be avray are Sli (Teoh oe potitios 16 Ise wight wot) we had aco elencent Whids Gb lee Mao ATE]: hes, Se ebenmest ATT is Placed ds Ie array, Next % Ie Laver elensest found “Citing while Loop io 5, Ager Wns. ) 4 op shebing ersecle te ie aight Ao atletmate way of cteing tee same ic IF Ppeeeeh vate Th Cot, Ay Ti-w) Pemerthy Pee TG SEKS IT” 4 melt val 1 wntegey > begio eee begin gees wile Ca CzI < atj-9) as begin Surap Catg7, 40j-); VERY end and end | WWW igeens.7 Topud array be Re, 5, 97,1, 61,11, 59. This is ‘otevprebedt as a Bioery dee ar geveo beloo: AVI J > Re Ata} 5 AT3] > 54. Weap cot procees + —— © BF A VS aa él 34 > e! 59 VY oN WAIN ZN Y t 5 Ut ge 4 gs it} BR Inthad Heap Sovted 2 FF Heap sre+ F ® SI Ay AS Va ae SF = 26 BY VN 1 a /- | £3 Sorted: ae, 54 61,97 Oa 4 AN 7 5 a ma B® } Heap Gre 2 Swrted + (1, 26, 5%,61, 97 9 No © ~ a 2 © a Ueap fixe» 2 } B Setad : 15,11, 56, 59,61, 4¢ ————— y 4 ae my Xp cursive Poe afe sot CAS. Se Poceduw usop9 (vax n,n + toteger) ; Va4 h: Boteger By begie 4 Cres n) Wes begin Bi = (mega) Jas MRT Cm, b) Ce Msor; Ch4tjn); MERGE Cm bind; end t end ; i Srocedune MERGE (Var db, Wre, ub + hoteger ) ; Ver Ee toteger S B: arney Td) of emerstyje > begio v 7 whl CPL. eel aad, 7 Ub) do begin 4 Catey argay begin Tk): = ATGI; Ste jae: Kis bers = Act flexi oats ta loop hel (feeetere ye begin Sky >= ated; Tie Kos bare end volile Cfs-un) Ain tor tem te i ub ote AVWT:= ateq; | * end 5 Sh pkect = Repdaeshe sill Guay paceie a irene Ubnately Livide Be Ble eg Fee ee Le curled ils nn Blo af lengté/ z F is Fixe 4. Procecime monge pettome MWe merging af NS Sorted suetiles ang Bally ve hires We Sorted File. ! fq: BS, 57, ae, Sig LED El, 86 22 ' ni Ae Be) fey Ge] [4] (2) Gay [e+] Ba) af) ake. 57) [aa ae] (ia 12) [as ej Pass 2 [35 a7 “,, st] [12 22 ge aa) Fax 2 (a as vi S&S OF Ha Ww igeers.7 A Heap allnas very eddteces} tnoplensers tates 44 aly Gueue. C4 Priomthy Cfueue fy a Speetal fupe ef Guu 25 Whids clsacls cvs Le celeler ace, ovding Us dersest gy Be min- Operatons 4 opuk cmay F< Cicer Serf be ne ‘yy! wills Program Nany I sertion and deletios tp L. Priory. Eriser we cars aaa We roax. etensest ). 4 Heap allmas boli’ traplensested %2" O(log n) Wo, 4 Heap exc be boplersected iio Ibe thel¢ “Bing Be Sequeshial Iho plenceota tina Eves eee beap allnar ao © Crobegs) Pex borroed > a tesuesce 4g elemesls ef AdAitonad space mequlred oly tor ables” B WWW eens. 9 is a technique which makes the process of searching easier by storing the records in a ic manner. In hashing, the set of records forming file is partitioned into a finite number of own as buckets. The address/location of a record with key value ‘ke is obtained by 1g some arithmetic function h(k). The function h is known as hash function. We can say that k belongs to the bucket h(k) and h(X) is the hash value of key k. Open Hashing Suppose we have B buckets numbered 0,1,2,...u.B-1. Then we have an array called bucket table (hash table) which is a header array indexed by bucket numbers 0,1,2,...B-1. All elements having. the same hash value ‘’’ are stored in a linked list and this linked list is pointed to by the i-th header. ‘That is, suppose we have a set of elements with their hash value h(x) = i, these elements will be stored in a linked list whose header is in the i-th entry in the bucket table. 0 | aeene | Deo 8 fx/ were} HOR «nent of i 4 o e000 Jx/hoos wry Bucket Table (Headers) Fig: Open Hashing data organisation Eg: Hashing Function h = mod 7 Here, the Bucket table will have 7 entries with indices 0, 1, 2, 3, 4,5 and 6 (corresponds to the 7 remainders when any value is divided by 7). Let the elements stored in the table be {1, 15, 26, 31, 76, 243, 550, 20, 281, 300}. The hash table organisation will be as follows: a * t e o1{ Us 28) Py + 2 Y {31 4 bb —[5s0} s = ao 2A 6 + | G5 too | feeala| dhashing WWW LOE ONS.. hash table keeps the elements/records in the hash table itself, As a result, it may appear can put only one element in any bucket. However, associated with closed hashing we have a strategy; if we try to place ‘x’ in bucket h(x) and find it already holds an element, a situation “tet collision occurs. Now, the rehash strategy chooses a sequence of alternative locations within the bucket table in which we could place x. Collision Collision occurs in closed hashing when two keys/values hash to the same slot/bucket. That is, when we try to place ‘k’ in bucket h(k), we find that itis already filled. Now, we have to choose an alternate location to store ‘k’ without replacing the value already stored in bucket h(k). This process is called collision resolution. ‘There are a number of techniques available for resolving the conflicts created by collision, Some of them are: 1. Linear hashing (rehashing) In this technique we use a secondary hash function, called rehash function on the hash value of the item, The rehash function is applied successively until an empty position is found where the item can be inserted. Examples for rehash function are hi(x) = (h(x) + i) mod B Gx) = (h(x) + 4c) mod B, where > 1 For example, assume h = mod 8 and with the elements {11, 8, 28, 19), the closed hash table with linear hashing (rehash function used is /u(x) = (h(x) + ) mod B) willbe as follows: oO 8 ! 2 3 i 2 | 5 s 19 6 4 Initially, we assume that the table is empty. If we insert 11, 8, 28 and 19 in that order into an empty table, we find that 11 goes into bucket3, 8 into bucket0, and 28 into bucket4. When we try to insert 19, we first try h(19) = 19 mod 8 = 3 and find bucket3 filled, Then we try ii(29) = (h(19) + 1) mod 8 = (8+ 1) mod 8 = 4 and find it filled as well. When we try ha(19) = (h(19) + 2) mod 8 = (3 + 2) mod 8= 5, we find an empty space and put 19 there. 2. Chaining Here, all values that hash to the same bucket are placed in a linked list. This technique involves adding an extra link field to each table position, Example: Assume h = mod 8 and with the elements (11, 8, 28, 19}, the closed hash table with chaining strategy would be —= - dhashing (WW. TCE CAS. 1 Pd hash table keeps the elements/records in the hash table itself. As a result, it may appear je can put only one element in any bucket. However, associated with closed hashing we have a strategy; if we try to place ‘x’ in bucket h(x) and find it already holds an element, a situation ‘4 co..1sion occurs. Now, the rehash strategy chooses a sequence of alternative locations within ucket table in which we could place x. Collision Collision occurs in closed hashing when two keys/values hash to the same slot/bucket. That is, when we try to place ‘k’ in bucket h(k), we find that itis already filled. Now, we have to choose an alternate location to store ‘k’ without replacing the value already stored in bucket h(k). This process is called collision resolution. There are a number of techniques available for resolving the conflicts created by collision. Some of them are: 1. Linear hashing (rehashing) In this technique we use a secondary hash function, called rehash function on the hash value of the item, The rehash function is applied successively until an empty position is found where the item can be inserted. Examples for rehash function are Ii) = (HG) + i) mod B h(x) = (W(X) + i*e) mod B, where c> 1 For example, assume h = mod 8 and with the elements {11, 8, 28, 19}, the closed hash table with linear hashing (rehash function used is Hix) = (h(x) + ) mod B) will be as follows: oO 8 I 2 3 11 “a 28 s 19 6 7 Initially, we assume that the table is empty. [fwe insert 11, 8, 28 and 19 in that order into an empty table, we find that 11 goes into bucket3, 8 into bucket0, and 28 into bucket4. When we try to insert 19, we first try h(19) = 19 mod 8 = 3 and find bucket3 filled. Then we try hi(19) = (h(19) + 1) mod 8 = (3 +1) mod 8 = 4 and find it filled as well. When we try ha(19) = (h(19) + 2) mod 8 = (3 + 2) mod 8=5, we find an empty space and put 19 there. 2. Chaining Here, all values that hash to the same bucket are placed in a linked list. This technique involves adding an extra link field to each table position. Example; Assume h = mod 8 and with. the elements {11, 8, 28, 19}, the closed hash table with chaining strategy would be * + DATA IRIS Some simple hash functions ‘A good hash function is one that minimises collisions and spreads the records uniformly throughout the table. Most hash functions assume that the keys are natural numbers. Various techniques are available for generating the hash functions. 1, Division hash function Here the bucket number in which a key value ‘k’ will be stored is determined by taking the remainder of k divided by B, where Bis the total number of buckets available. For example, if there are 20 buckets, then h = mod 20. Now h(k) = k mod 20. That is, h(115) = 115 mod 20 = 15 19 mod 20 = 19 h(80) = 80 mod 20 = 0 and soon 2. Folding hash functions In some applications, the key value is not an integer. For example, in case of insurance numbers, the folding technique works better than the other techniques for creating hash functions. In shift folding method, the insurance number 567-96-1505 is viewed as three separate numbers to be added. 567 +96 +1505 = 2177 ‘The above number can be treated as the hash position itself or further hashing techniques such as ‘division’ can be employed on the above value to geta final hash value in the desired range. The shift folding method has great advantage for its ability to transform non-integer keys into integers suitable for further hashing actions.

Вам также может понравиться