0% found this document useful (0 votes)
56 views

Algorithms and Data Structures.

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
56 views

Algorithms and Data Structures.

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 10
“How much time it takes to vun a function as , : Abate : Raine, PIA Ep “ number of elements Const avvayl = [OBB &, Pl n=5 | (eh count=0 | fowler i205 haystack.lengen j ite )2 ' , FChaystack Lil = needle) Count += 1; | 4 Yetuyn Count 5 CL How tong does i+ take to execute when o> +he number of elements (in) 15° ¥ axeustion t L% woe» this is time ayows _ linearly, as OVVOU, SIZE incveases + (sw ut aul) M\ - : Let's see if we have Some Function Hat doesnt 25 or Array size (nt Ww, has noeffect t ? : ‘4 1 ‘ ow) ola) w> onthe runtime time, inns 1 5 10 20 223 Constant time u ow), a . . . 1 $ Quadvacric time = OUD pes nowever oe ne rane Const ronal arco, r= £4, &, Gs, P71; Ene w Const hasPuplicates = Covy) > t a re for (ler t=03 T< avv.length 5 V+) [00k ! leh item = aw L113; nen iE (avy. Slice (Yet). indexOF (item) t= -1) vertu ; ‘ , weruwn tvue; RD prover \ > 5 vetuyn false ; owe \ork uP 1 a ata Hructuses Array & Linked List Array a linear data structuve, Stoved in Contiguous iar memory memovyy locations. Addvess 300 oso 208 fray CO] [1] C2] [3] " Assume each & is an integer) = = Vequires 4 bytes space | “y v The array of & must be A, allocated Contiguoursly Sor" > addvess 200 — IIb ve. BP ya4l yw can fandomly, Acless wi inde¥ atz] — Vv Contiguous = No extra werory allocated = no memory overflow! OA a 228 poe CF meh! - ° Fixedsize.. Lavge space oy not be avat| fov dig Aas = BD took the space! § @ Insert + delure elements | Ove Costly . > may nLLA tO create 0 NEN | Copy of the OVO + allocate ak anew advess LL NS | Array & Uakked Lise 2 & |ineov data Structwe. > & each element is 0 Sepavared object elements ave linked wy pointers aE memory Pointer fs Cradvess of the ee next node ) oes" 420 — a ios B — _addvess 748 Unlike On orvey, Linkedlist A , elements ave not Stoved in Contiguous locations . | és. You pe CF meh! foo acy ae Cipriani cia. @ No vandom access =s12e Can Growey ShYINK | Wremov v Insert + delete element | Need to-tvaverse ave Flexible . wn kimes \ oa = no need to shift nodes like avvay insertion V memory, is allocated ==»: & Revewse dvavevse or Yantime 1s hava 410 7 time Complexity iS O(n). aVaY, iS Ou) | ° & @uat> A Stack is & LIFO (Last- in-Fivst-out) 9 data Structure, where an eluent /P* fe added \as+ push) gets 722) Yemoved Fiver cper) 2 it tw Just like a Stack oF ice cream scoope ! ia omg, the bottom eve Stack AS Gh GYVO4, in Las| = 7 one isalways _ OYVAYS in SaaSeniPr muting ! \ | bet Stack = C1; a ave dayne ack (st : i stack. push (‘mint choc’); 7 Dmnt choc’ 1 1 1 stack. push (' vanilla’); YW eimint choc’, ‘yani\loT! Stack push('stawberry')) / [mint choc, ‘vanilla’, | | let eaten = stack. popl)) % eaten is ‘strawberry me 4y is OC") paar ! v iow oe Tach. me U wink choc‘, ‘vanitlel) Sick: & ®) © Qu2ue is a FIFO (Rirst-in-Fivst-out) dota Structuve, where an element added Fivst (= enqueue) Bers vemoved first (= dequeue ) dadquore “[_} just like waiting in \ine ot a popular vestauvant t Nyan — ynavu Business Cox Grumpy cor wi, mss) “oe ron 2 Stack OS ah GYYAY, in a. eS" C eee ' ‘et queue=L 1; quow. is! oe i we «x | queue. push (! Simba')i Csinnba’3 ae 1 | Queues, push ( Nyan'); 7 L'simba’, “nyo \ Queue, push(‘mava')) 7% [ ‘Simba’: Nyan', ‘Wmaruy! et €orev= Queue. shift (), 7 eater is “simmbo iv Time Comploxity, queueis Linyan ‘, ‘maru' 3 Should be OL) fov both enqueue + dequeue but JS shift{) is slower! 3 A hashtoble is used to index large Amount of dota 2¥ QUICK Key-valus lookup. OCI) on average Ly Faster tn bvute-fovee linear Seaych my © Let's CYeake Gn oO Colculare the hash Ovvay of size. | ee by Using the We've Q0inG tog | Yh. Talon", add Gdota. 3 jp 2.9, ASCII Code, Mp5, Sak Kel = “Tabby” puke values*pizzas —|t = 6 > &#) - 530 a hy Supe soi une ; emer Has! @ Lets add move data, GD el > 353%5 =3- Tux oy, @f#] - 301% 522 Bob @ Use moduto to pick a position in thetaviaty Cb 2 8H > 530%3- 0 ‘ 71 \ >| Toby divided by ne Bob Size oF the orray, . The vemainder is the Position! Colliston! -@ Hash Table Now we wantto add move daca. Let's add “Bengal ". 6 Ch “vergal"> OE] 617%5=2 | But Lal slot hes been taken >” BS by “Bob already! =collisiont 3 |S so let's Chain Bengal next 4 to Bob! = Chaining key: Bengal’ — “Sphinx” keep Values" posa® “Fishe Baatng Chips dato . Let's \poKup the value Foy Bob" | ® Get the hash > 307 — 0M \ | 1 | @Gex the index Z01% 5-2, | | @Lookup AVMALIT > foundt 22 | FE Let's love up “munchkin” eS vos | | © Hosh > gol ae 4 | © Index > BbI% b= 1 ran ce | @ preg td - = "Manx" | \ \ @ aire -mac Dia duwes Binary Weapy & ir vee < Binoy, Seoveh tree ate hob seer. Binoy, henp = '& e0ch node Mas ot w Compre tree ~ . most 2. childven * Min NeMp oy "OX x haoe 2 % used for Priority, AuLUr “ty Maxheap hoop sort eke wt 0G) the Voor in qvvay 1@ Dye nas the YO lager — [olelelell 304 Cin mtg ) Coy Cia (24 031 041 v each node has 0-2 childven pe v always Fill top bottom, ler ight Low brim 1 Addtothe next node Golelaes EEL: @ ~ é & tree jain shuchure. CinanpSeavewiiver: ‘ka. Ovdeved ov Sovted & €0Ch node has ot w aKa. Ove mos} 2. Childven binary vee : v Fast lookup de Rule of thumb e4. phone number lookup table myname A * €achvalue of all nodes wn he \eft subtvees is lessev A's lef subtrees : 8,3, 4.7 a . 3,7 Smollev-than povent * Coach value of all nodes in the iN @ Sorted ayvaYy,: Vigint subtyees 1s lavqer SUeahonpps| * No duplicate Values “<7 Inserkion > Alwoys add to the lowes+ spor 4obe ateaf 2 No veowornge! Q Let's add@ 1. Compave WI the voor Fist. 2.@®<@® so go lett. 3. Hnen compave w/ the next, © 4 @<@ 5090 left Gomplexity: 5 Compave wi the © Ave. OClogr) b @® >® So go night Worst. On) 7. Compave / the@ ; § @<@. soodd to the left . Done,

You might also like