Data Structure

You might also like

Download as pdf
Download as pdf
You are on page 1of 11
“How nuch time i+ takes to vun a function as , ¢ . the size of the input grows." Runtime tee ao Orauy number of elements Const OXVayl = LORS. &, Pd n=) ! et count= 0 i | for Cle iz0; haystack.length 7 ite)? 1 |, WChoystack til 2 needle) Counrt=4j | 5 yetuyn Count 5 G How long does it take +o execute when > 4he number of elements (n) 15° his 1s v execution = \ 6 art . time gows = > Linear hme linearly, as = Comploxi OVYAY SiZe & 7 “ incveases | : ; Ege ° Let's see if we have Some € Function ak doesn + 6 er Anaysize ,% OW) WN? wo has noeffect t won the vuntme Hine, 5 in 23 Constant time u oo), $ Quadratic time =.OGwd os, noweve™ oe ne rancrimne Const \ cre orvoyr= (04,8 & & PI; “wn? © const nasduplicates = ow) > t vO Vi ' foy (let 20; T< avy. length j 14+) et | let item = av L114; oe So slice (¥+1). indexOF Liter) t= ah) verun : \ eel \© pnother 1 > etuyn fal ovvaay- LookUp 3 Mas wl indexoF method data cthuctures, Array & Linked Lise Aha & lineay data structuve, _ Stoved in Contiguous dur memory memovy, locations. Addvess 200 golf 6208 AIR prvay [OJ [1] C2) \ = Vequires 4 bytes space v The aay of must b& allocated Contiguously > addvess 200 — IG Le. pacer CR meh! - Bye To @ Hyxedsize.. Lavge space ¥ can Yandomly SMa, nok be aval ov Dig Away, acess wii indeY 2 took the space! § ate] — ey © Insert + delare elements YW Contiguous = No ext awe COSY . mero allocated= > MAY nLLd tO creme anew nomemory overflow | Copy of He OVA + L allocate at anew adyess Lata Kructuses lis Arvey & Uhalsed Lise Lg linear data Structure. > & each element isa. Sepavared obsecr + elements ave linked w/ pointers dar memor'4 SZ { int e és Craaresso the node oe fe fe next ni . —— woes 400 324 -& \35 a "— address 248 Unlike ON ovvay, LinkedList A , elements ave not Stoved in Contiguous locations Yay! pe CP meh! fo Senin . y Dupnamic dato 2 $12e-Can grow ev Shrink a bie vitae ALLESS Y Insert + delete element Neod +o tvaverse ave Flexible . mimes \ nat 420 = no need +o shift nodes. > time Complexity S| like avvay insertion V momory is alloted 69 Reverse vovievse ot yuntime is had ° & @uan> A Stack is a LIFO (Last in-Fivst-out) rs dota. structure, where aneloment PS FR added last (push) gexs [ex vemoned Fivst (= pop) 4 si Dw Just ikea. Stack oF ice cream scoope ! qanoxner 5 From @> 0P por f wo : - omg, the bottom aw Stack AS an ara, ns — “ onersatinys — ' w OYYAYS in JavaScript melting th ‘heb Stack=L 1; eee Sack ts: i stack. push (mint choc’)i / L'ment choc’ ] 1 STACK. push (‘vanilla’); / Cimint choc’, ‘vani\leJ (SHACK. push (' strawberry") ; 1 Lenin croc, ‘voniiia’, | lek enten = stack.Popl); Heaten is , “svamrere) 1 2y Time Complomrty is O(1) “Strawberry Sov both pop + push. L' maint choc ';' vanilla} eoBiraels (Sra A Queue is a FIFO (Rirst-in-Fivst-out) dota. Stvuctuve, where an clement added First (= enqueue) gers vemoved Fivsr (= dequeue ) doquoue “[_] just like waiting in ine ak a popular vestauvant \ /) OD Bey b ys D () 1 ] ee eo) Nya aru ; Guan aX Business Cor Py 4, 3 & Pee . / we ON pd es ide Stack 05 Gh GYVAY, in we ok Ae | [ XU o&X I jer Queue=L J; quaut is! eH Ge! ' 1 Queue . push C'Simba' di W [isimba'd wh Ok i roe . ! Queue. push ( Nyon"); Listmba’, ‘nyan’] quexe. push('marn')) % ['Simba'c yan’, ‘Maru! let Corer = Queue. shift () / eater is ‘Simbo.' iv Time Comploxity, queues Linyan ’ ‘Mav’ \ | Should be OU) fov ! both enqueue + dequeue but JS shif() is slower! 2D A hashtable is used to index large amount of 2¥ QUICK Key value lookup. OU) on average Le Faster Han bvute-force linear Search © Let's CVéake Gn oo Caloutate the hash OVVOY OF SIZES. | value by Using the We've going 5 Key. “Tarcoyy", add GH data 3 2.4, ASCII Code, MD5, SHAL Kel = "Talo" | pa vauer pizzas N GS 2 GE) > 530 some dace Hash ' , foovite Lea's Say: fo! ‘rood! @ tse modulo to pick a | position in the OYVOYy, CS > GH] > 530%5=0 4 7% . [oq )] Toby <1 ‘ae The hash is divided by, te SiZe OF the orrouy . The vermainder is the Position! & eH] > 353%5=3- Tux é EH BH) — 301% 5a Bob Collision! “@ (asin Table Now we want +o add move dada. Let's add “Bengal ", CD “Bengal” > 3 611%5 =2 Put [2a slot has been taken by “Bob‘alveady! = collision! so let's Chain Bengal next 40 Bob! = Chaining key: "Bengal” — "sphinx" eee 8 Of eg! “Fisht addi Value: " Dosa chies" sate Q Seavching for data———-, eer (pokup the Value fo Bob" | | OGerthe hash > 301 | — 0M } | © Ger the index = 301% 5-2. | | @Look up Array, [2] found! 2+ . | {Let's look up “munchkin” oN S | | O Hash gol Lean | @ Index > gel%b= | a Eb | @ AWOYTAY > “yaanx" _ ; \ ® Operate olineav-Seavch +o Find Wundkin | Ke ke, Average O(n) { @gire-mac a Binary Heap? Bi” Binavy, seaych ~- yg ean Binoy, hemp = - Resch node has or ¥ Compre tree” most 2. childven v min Be oy MX ‘4 @ used for priority, Amur -w Mae heap K2OP Sort a — the voor in ovo has tne ravgese «= > (wl Je Te T3 NN Pe!) Coy Cty £23 03 041 v CAC node has 0-2. childven 2,” » always Fill top bottom, left— right «<<, boton — + 4 Addtothe oe node QW 6% oh. no! Always add 4v the lowes spor tobe ateaf B No vearvonge! Let's add@® 1. Compave WI he voor Fivst. 2 @®<® so go lett. 3. nen compare / the next, O 4 @<@® 50 90 left Complerit4: 5. Compare wi the @) — Ave. OClogn) & @>®© S090 vignt Worst. OCH ) 7. Compare “/ the@D) 8. @<@. soadd +o the left - Powe. Refer link for more

You might also like