You are on page 1of 12
Eig? “How much time it takes to vun a function as ; : the nze of the input Brows: Runtime co Oro number of element s Const orrayn- (OR @ PI ng | forCleriz05 haystack.lengtn j itt)? i : iF Chaystack Lid = needle) Count +=1; 5 Yeturin Count 5 4 How long does it take +o execute when [> the number of elements (n) 15° ¥ exeudtion time Ayows z linearly as = OVYAY Size 7 increases + : @ Hinie_mat gD rious =a Let!s see if we have Some Punction that doesn + ee eee 1 const always TrueNoMottey What = Cnayskack) §} 1 vexuvn tur, 25 7 Array size Ww we) has noeffect ic won the vuntime ime : inms 223 Constant time ul om), a ; ; . : $ Quadratic time = OGwds ae oe tne runnin Cons is + aroyr= (8.8 &,& PI; ene © consi haspupicates > mst Oy : for (let t=0; B< ay. length } Her) Lore ' bet item = aw Lid; SNe Occ 1 (ota ee Ci 1). inclexOF-Citern) t== -1) ti fae veruvn t+vur;, © pmother 1 | vetuvn False ; ovvan. lookup! 5 wl indexofF method 1 ’ ata Sructuses Array & Linked List Aap & linesy data structuve, Stoved in Contiguous dar memor 4 memovyy \Ocations. Addvess 2000 964 208 ANH prvay LOJ Cl] €2] [3] (" Assume each & is an nese = Vequives 4 bytes Space ey Y The avray of must be ° allocated contignoustyy | > addvess 3.00 — IIL ve. - Pda @ Fixedsize. Lavge Space v Con tandomly =| mau nok be avail fov big AO ACLESS W/ indey :@ took the space! £ ate] — ey © Insert + dere elements W Contiguous = No extra Ove COSY . menor allocated= > may niLdato create a now no memory overflow = Copy oF ne WV + | qllocare at anew advess . peepee nec Cemeh! : Lata Sructuses lish srrany G Ualived ise * a \ineoy data Structwe ~ & €ach element isa sepavared obsect ¢ elements ave linked w/ pointers dae memory - inter o oS B Fadvessobwne node — und ar — ee a nul ‘ j 4 * ( ( (By D 0 260 B -4% Unlike ON orvay,, LinkedList A ~ elements are not Stoved in Contiguous \ocotions . €%. (gory! BX G meh! y Dynamic dara. k @ No vandom access = SIZE Can Grow oy shvin eens ) 1 | V Insert + delete element | > Need 4 1vaverSe | I 1 | ! | 1 ! no neod toshift nodes | 7 time complexity iS like AvvaYy insertion On) . AYVaY, is ou)} V wmovy, is allocnted 6 Revevse tvavense or — is hava ave Flexible . times I A Stack is & LIFO (Last in-Fivst-out) daca structure, where aneument Per RE added last push) gexs ial = iam = vemoved First (= pop) LW Just like a. Stack oF ice cveawn scoope ! * / omg, the bottom vt Stack 05 an ara, in Los noose one isalways — ce OWVAYS iin SavaScriPr wlting A ! feb Stack=F 3; MOP Sinck Us! \ Stack.push C'mnint choc’)) / Cimint choc’ I 1 ,S4aCK. push (‘vanilla ); — / Cimint choc’, ‘Vanilled 1 S4ACK. push(' strawberry"); / [mint choc, ‘vanill ! a, 1 ‘ val : ek eaten = stack. popl)) 4 eaten is a rt x Timec : “5 O(1) SHVOAWPRYY' 1 t Y ay boar ete bush. L'wint Choc ‘vanitte} ata Structures (eaguOas E € A Queue is a ELEO (First-in-Fivst-out) dota structuve, wheve an lament added Fivst (= enquere) gers Vemoned fist (= dequeue) just like Waitting in \ine mS ot a popular vestaurant | wwcomney § Business Cor Grumpy a oP o wy : ve Vast : 2 Stack OS an avravy in Ld: esr oe ee ) pbk XC ‘tet Queue=E J; quan (6: ee | | queue. push('Simba')i “ [ simba‘d wh ok Queue, push (' Yan’); L'stmba, ‘wyan'] queue. push (_ Mavu')) % [ ‘Simba’ yan’, ‘Movud \er Corey = Queue. Shift C)) 7 eater is ‘Stmbo' Z¥ Time Complexity, queueis Lnyan | ‘mara’ J Should be OLN) fov I both enqueue + dequeue bur JS shift) is slower! | ne @) \ | 26 A hoshtoble is used to irdex large amount of dato. 2¥ QUICK Key-value lookup. OU) on avevage L> Faster tian bvute-fovce linear Search OxVay, oF SIZES, We've going to add & data. Ket = "Tabor" Values “Pine : some dao a , fovovite Let's Soups fou food! ® Use modulo to pick a | position in the OWVOYy » CD > ez) > 5630%5=0 roe ¥ $3 [oF @] tw, < OF] — 617% 5 =D But Lad slot has been taken by “Bob" olveady So leks Chain Bengal next to Bob! = Canning key:“Bengal’ — “Sphinx” value: posa" “Fish* Chips” Q Seavching for data—-—— “& Let's lnoKup the value for Bob” | OGerthe hash > 301 | @ Get the index 301% 5=2 . | @ Look up AYO, [2J - found! ot # Let's look up “munchicin" ! | | | | \ | x ® Hosh 7 gol ' a © Index > BLI%S= | < feat “manx" 7 ® AWOL = ® Operate olineav-Search +o Find ‘munchkin a od ! = Collision! oa ane date @girlie_mac Jaa Sauer eiry] Heapy york a7 Binoy Seaveh tree ‘ wo £ & tee dota. shacure Binoy, heap <+ ° (®& e0ch node has ot ~ Compre tree * most 2. childven * min Nap oy MoX heap 5 © used for priority Quer -% ntl hLop Sov+ we eee na mee IRE) 389 @O+ Cminmeap ss Coy Cy £23 031041 Ane opposite » ) - v each node has 0-2 childven is v always Fill top> bottom, left vight <—_, bute Jy Insertion Let's add 5 to the heop: °@ 1. addto ! 2 | the nexr ‘a 8) node - *@OESa, compare s Add I~: 1 Addtothe (9) nevivanae 2 Boe) EE" OOO" on.no! the povent issmattey tah its Child ! Swop-them 11 | w/ (tS parent (ole [eo [2/2 [5 [7 _ 3, the paren 5S. gyentey, Cool, (t's done! Dad 10 te next Lets add mow! [els [efo|a|st Tod Cid Ce} 033 C4905) : | od ¥ q\ node + vepea $oow the process @ WaREP ELE Coirlie- mac Anta Aucwes Binary Hd =4¢ Heap Deletion ea TE you wore to delete the last ear, ies delete it ¢ = = ge ‘ye. 4, ome 5 But typically, You would delete voot + a @ 1. Remove the voor MO 2, Move rhe last eat BOSD er OAhe voor SQ port! ‘ hala tals — Repent. as voor. Compave!l @ or Ph Swap it with @®GOO she childven the lovaey ae oe on, q 2 a Gila, ana EFA AEN E Mild % a OF, Teme Complexity yD wipe neat eee palolal i ow log) "000+! + Pelee O.Clogn) OCiogn) (a[s] mea) > [5 |e] ee ay @ Qirie_moe ® ". Binopivee Bron een beienrce Peo oa nowy, Seavc| é Xtree Conte _ a sen 4 tose & ech node has v oka. Ovdeved or Sovt mos} 2. chi ‘aver binary tvee v Fost looky det Pale of-thumb 9. phone wutebev lockup table by nome * €achvalue of all nodes in he left Subtvees is |essev A's left sulstvees :8,3,9.7 A®: 3,7 —Smoller-tnonpovent * Cach Value of all nodes in the in a sed ms vight subtvees (Ss lavaew # no duplicate’ Values 3 Insertion > Always add 40 the lowest spor +o be aleaf 2 No veavorge! t Let's add@ 1. Compave WI the voor Fist. 2 @<® so go lett. 3. then compare w/ the next, © 4 @®<® 50 go lett Complexity: 5. Compave wi the @) Ave. OClogw) iE @ >® soo right, Worst. O.Cn ) 7. Compove W/ the §. @<@ . soodd to the | left ! Done. @qirive mac navy, Seaveln Wee ¢ Ae Data SoC (BcsefGroien™D <> x i Cose 1: the t0-be-deleted node has © Deleon | no child Case 2+ the node has 1 child case 3: the nmodehas 2 childven Let's remove Ql) < Case 4. 4 has no child. So just vemove it from the node. Dorie Easy O5-,. Now. let's delete M < caser 1, jUSt VeMove I+ ' a then move tne Child.@ to the spot! Now, let's delete® Repeat the process! ) ED be Find the lavgest from Complexity jek SUtree. Moveit At Cm - (ey Find the lavgest Worst. O(n) Fvown left sulstvee

You might also like