Download as pdf
Download as pdf
You are on page 1of 37
“Time of the Session: _ roctab Tasks Saas cet Vi wn a ae a In pia. So, help Brg Vishal 1 range corps socordingy (Imagine corps height as elet valyze the time compexity((0 108 #)) so: serio sort beips to sor array in best ime complex Hincludecstdio-h> vad wast Cint oC), nto) 2 gant U3,k temp: Ferct> wien: tt) z ae tom (32 15> 0757-7) at Corse ats) remp ois oi} ofi-\]s mn ym) 5 Roth (epg ; otm)) 5 Rintt @ “y"),, t Com), _ 21SC1202 - DATA STRUCTURES, aatist of N array element apply shell sort. The frst . 2, Given ae 2 peace ioe commie spece-scparated integers. Print the array as a row of spacey in qgers in cach iteration. ve Programs Hindlude < st dio h> Aloid chelll Cant of 3, ot 9) t gat Ui temp. g P> pon(gente: go ogr ale) 5 Datequene > Pa atst de: geree & ap (atsteats-) 8 semp=alts afi) -a0s-9) af: qi= Aemp> i Bn (P20: Pens ptt) Pitt (reted*, of Ps Pratt ("WO") Sat maint) g Sal ny Scand Co bl E> int a(n] ,ts a (eosienit*) Seon} (ated £903); i shet\Caw):, eee 215¢1202 - DATA STRUCTURES, snstab Task 1p Jimplement Insertion Sort 1 of Nay elements ply ination aon A Input Format ‘he fis line coma ‘Tre net fine contains Constraints 2 1=N<= 1000 « the integer N, the size of the array N space-separated integers “5 -1000<= afi}<= 1000 «put Format: Print the array as @ row of space-separated integers each iteration nges/3-a-implement-insertion-sort hteps//ww. hackerrank. /contests/1 7cs1102/challet Pra sHinclude word ainsit Cnt old, int 0) t gat UK temps Domi atte nytt) ta (ets 37085 py ae@uyesOD) £ Aemp=els)> afi} = ots ata-i) > temps ei Don (ero er E* *») pintd (rea “, oC kY)> printt CVn") 5 int moin( ) a snk n, lms Scand creled™s BN) 5 tat aly); Prlleoy lens iy : “elements apply Quick Sort, ieger, N. the umber of elements in Array. att ako integers each iteration oa Hincludeestdioh> Nlotd quick _gont Crk arm (res) Rot vad» seat Paar tition (int artes) ant fat); Gat main) £ fat orifices. ts patot? (Enter Yhe remben 4 elements 4 sconf ("ea &79> pret £O Enten kd Aements era's)? Ry Cieos tens aaa) a sconf ("elea", Son ))> -Sod (ar, om-)); (rte Saited Annoy ys ew") terns tt) Merge Sort ist of N array element apply Merge sort. Merge Sort is a Divide and C Fr as gael forthe two halves and then merges the two sorea wake wen ivi talves, The first {i =e ine ¢ ace-separated integer p ena li yy in two ha 9 he numberof elements in Array. The second line contains N ecg arow of pace-separnod integers in each iteration, wl ckerrank.com/contests/17cs1102/challenges/merge-sort pram: Hinclude zstdio-h> oid merge _sot (int int): Aloid merge — Arxay (tat int pint tnd: ee d iat avsCioo): Nicid main ()$ Satin: pat? ("xn Exiten number os alements om) - Seon? ('*]2A, 60): Prt? ("Enter Elements: so") + Deiceo <0) sont ("ted £onli3)? Meee! Soni (O,m-))? pint ("~~ Soxted Datas")* Bits ten. 1+") E Bink? ("4 3a", onl) > a Noid menge - soit (int L ant 1H) £ aot om; such) ¢ me O+h)25 menge - Sort (Lm)! merge — Sock Cm), 0)9 I Merging up arnays menge ~ srony CXpm, mat) 5 A Nea rmenge -avray (tat @, tb int Gyaae - 2 Sat Woods Jot t20 320, kro? bile (ie=b aaie= dt) tu th Conti e or )) afer} = anfitt] + else afer} = on 04)s I Collect oe! elements while (te-b) t(k+4] = anfitt]: while Gerd) pom (ite Fees cane +) Noor and his pond ; ming. There are N types of fish, Each type of fish has size(S) and eating factor é a tized as well as they do not cat cach other. 4 is mani esas comcast Be Slaten rogram: pp include stdlioh> 7 a ysck_tok Cot *a,iot bat viet Loot) 2 sat Vy emp Pivot: ib (put lost) : Pivot — bouts 4. frou: 9 = hawt: : ushile Cotile a Pivot) z vats A while (ofal> at tivot)) Sia ihCey) temp=aG) arial 4 ail = emp: “epee ai}: af Pivot; arivotl-temp: quickSoni(o, bstj-!): q {ick sort ©, iv, host); Ynt maint) int n, Pivots Sank (ra £9); int a(n, i,j; fonCizozien:it4) £ Scant ("4 4", kel) awicksorkCa,o,n-1); Bee y PounttC a", ar); bs to pny a very exciting online card game, Atte Os ents decided 10 PRY 2°57 5 eat ye beginning of this game, ca ici. n which each rd ‘come strength assigned. After that, each player eae wig ahey compare SENET sree eked cards, The payer who picked card with larger sen ce orn players picked cards with equal strength, th wins. There is nner 685 giend got 8 deck Pees ata eres ras tne card ns rene wd. Seoond Sent ot » deck wi nae i with m cards, jove his cards. He can increase ds. by 1 the strength of any win very much. So he decided toi vee inpcoved at many times aa be wars. The second fiend cen {this possibility improve hit wants 10 or 1 dolla because he ‘Any card can doesn't know abou tee a victory for himself? sum amount of money which the first player ‘needs to guar si \ce-problems/algorithm/card- ort =j-s4eofeT/ ram i tinceludeestdic n> Ved quick sort Crnt cton{yos), Tat iA) # cat Fantsion C mt tot); fat main’ ) = rat cooled) int osertiec), nits iet-} (“Eriten the mumbest OF elements aan Nhe fos stant (tiled, 29 pratt (rye Enter “b Dy Heoten itt) Secon’ Cl a, Zens) 5 elements + yawnyo)? 4 quick got (om: on)? pia? (oN The Soled Aromy 8 eyeh?; n(a=0; ten 1**) J pivld (7 us", eA) metunn oy cr any em Te Be oy a eee on 21SC1202 - DATA STRUCTURES, Week-2 Lab Session: Date of the Session: __/__/__ ibewiidesn ie Prerequisite: @ Divide and Conquer Algorithm © swapping items in place © Partitioning of the array. Pre Lab Task: 1. Chris and Scarlett were playing a block sorting game where Scarlett challenged Chris that he has to s¢ blocks which arranged in random order. And Scarlett puts a restriction that he should not use reference o median and last blocks to sort, and after sorting one block with reference to other block, for next iteral must choose another block as the reference not the same block (random pivot). Now, Chris wants help fr to sort the blocks. He wanted to sort them in a least time. Help him with the least time complexity algorithm, progam:- =f includeestdio h> Mleid quicksortCint Fo, int final, ‘nt fost) int 1.5, temp pivot? ib Chamat eax) Pivot = outs fiuts je lets eakile (#44) £ hile (ocite= ateivot)) ee 4 eokile (atil >a(pivotl) og WSC ay) t gemp= ali}; atijso(ol: & Coqetem py 43 21SC1202 - DATA STRUCTURES In Lab Task 1. Jmplement Shell Sorts 1 ane OF array elements APPIY shell sort on array. Input Format oper t line contains the integer Ns the Se of the array. Te fant line contains N space-separated inteBers Constraints + 1<=N<= 1000 © -1000<= afi}<= 1000 ‘Output Format cane he array as a row of space-separated integers in cach iteration. Program: +pYinclude < Stdio 7 gd Sheth int af 1,209) t sak GU erR, VPS Fyn( gente’ qe? ea! t Jonti-g le nytt) S aacutseeg FED t id (ateots -3)) : % temp ats), otis aly fs -q\= eps a choy (PEDIPEMY P**) Packt rena’, oP» 4 Perit O40"); i ei 21SC1202 - DATASTRUCTURES s. Insertion Sort ac Be cristae posta: Ht include c Stdio’h™ Uoid “sit Cant al}iat 9) = grt UL K Amp Dortiztsier**) Ty arrirot 3D : eo (oriteot-V) Caren pol ac aysote 4 oer? (coke ey path red okt) BARONS)? Gat maint) gata siems Scan¥ ("tea £9); int ato}; Ry (sorkens sty Scan (lea RAT) dan Cmeormensm **) Pritt (eh d eel) > Beat? Cw"), josrtCa, 1); rg dts Troy te Pereriatae DATA STRUCTORES! : jous challenges in these next few cl sition Sort) Ths chal Jemented as follows: covered Insertion Sort which ich i 8 simple and allenges, were covering a re ee sy lenge is a fenene Ee eee aes cna Caen Ge Be ee ge ae algeoies el Gy cas peceneng ess partition ig Ws ll vallens x ami: fPinclude estdio-h> Alo quick - Sort Cant CL Wh and: ant Raottion Cort Ral at) + int meind) Tra ov Scan ¥(* gat aly Agcacor eT *D sront (1 8") gatil)> quick —Soxt (o,om-\)3 Anlizosiensi**) pint! (reed OCD) qeluan 0: i gat at, at boot Ce) Afoid quick sot () 2hd", 20)? x Rat f5 “Pow ctidr? : ie aannenntodoodigh quick sot feteese D> quick - Sst co. sat akigh); x act Rantition Ciatal J, 40k boo, et \sqh) § gat Piost jempree® howe Win) : Prot= Weqhs 215C1202 - DATA STRUCTURES st Lab Task: Missing Number aa are given an array A. You can decrement any clement ofthe array by 1. This operation can be repeated oe aber of times, A nme is sai to be missing ss the smallest postive number which sa mule ras got presen in the aray A. You have to find the maximirn missing number aller al ponte serements ofthe elements tice/algorithms/sorth : it “ 1 emrami: 4+ clude c stdio.N™ Mlotd quicksont Cint Fa Ant out, int tase int 1.3, Pivot Amps if (poate lok) © pvot= fuk: is fats j= lab while (zi) Ba while (oct) c= 0 Pivot) é ws a wkile (ocy\> oC rivet) Sac Wed ‘ jemy oC), ai} = oC is o( i}: temps erp = oC), OC) pivot; o(puot}. temps Quicksord ( on biouh3-\): a eh ——_—__————- AveD 4 int PIE, Oy, sont (rd 8 int oid focizoriensit) © sont rat Sats 4 quictiad (o,0,0-); | oo ‘ pitted" offs Ofte ee Oe 21SC1202 - DATASTRUCTURES, 2. One-Sized Game: Ladia and Kushagra arc playing the One-Sized Game. The rules of this game are prety simple. During the aoc play, if at any point of time, the array i 1eR with only one element, Ladia ‘wits the game others cushagra wins sf¥ea i es) Comming of N clemseus, print wether Ladin wil win or Keshagre eee actice/a -sort/oractice-probiema/alzrithm/on rogram:- =H include esto h> Void quick sont Cint Fo, int fuk, int Lert) int ij, Temp, pivots pf Chout clas) t Pivat= foods fous y= lets ahilelicy) © tale (ati) c -efrivot) uo te y cohile (otiy> aceivetl) ia: ve Gea) Saeemp: ots atid= ofeeat) al poy) = temp, ei 4emp oC: a(3)= 0 (Pivot); O(Pivot}=ter Pp 21S¢1202 - DATA STRUCTURES ‘Week-3 pate of the Session: _/_/__ ‘Time of the Session: __ to. Prerequisite: Pre tpivide and Conquer Algorithm © Sorting swapping items in place © Partitioning ofthe array. Pre Lab Task | Trace out the output of the following using Merge sort.10, 49, 32, 67, 45, 4, 7.2, 1, 51, 78, 34, 89, 87, 36, 29, 3.9, 11 GA, 454,45 241,50, 1B. B4y BI, 3G,? net 4 erations CED es a 0/49, 32” Bort \ fe] ua lao los els E phe ce pbs leelerls \>\ ul ° 2 aS ees a Senet 1 M4 aa|az|z |e? wos INS aN cELEENG 2 4 3% seas 44 \ \ i Jaa : es 4a é ep 3 PORE Tee ee ee In Lab Task 1..Quick Sort, ‘You are given an array AA of size NN, Sort the array using Quick Sort and print the sorted array, ane 19/pro! 061 PreaEAM 146rclude c stdioh> Jntvoid quick-sodCint *a, int Beat int tay) & int 1.j, Buot temp; Vp Cfaoutt ¢ host) Pivot = banat te fioutts ye tants esbule (ic}) ee (atile = ot pivot) ena 4 hile (ati)> 2 Pioe8) ey € temp= alts alij=ats); ats}= tmp; a semps af ide ats) = a(pivat); Alrivd}: emp; quick sont (ayq-\ hiout); quidesork (a, \41, host); ao int. main? on 1, plot? sok d DY; int olnJinjz font iz; iensit € Sen 4 ACY 1 cate quicksont (oon -1); fon (sofenyi4 4) ean in ("1d ae 4 7d", ati); 21SC1202 - DATA STRUCTURES Ma Pe A having N distinct integers ihe power of the array is defined as: 2 max(A[iJ-AU) where 25tN Tor each iis the largest index tess than / such that AUj}= votd quicksorit Cint *o, wet brut, int lost) jot Udlemp pools +P Cfpout eleak) . Pot = Bout 5 is fiouts j= Last; whi le (ici) Tale (ati = atpivot)) 2 at 4 while (acil> aC rivet) Nes if ie)) 4 temp= aly, a(i]s afd, Oi): temp, 4 4 Mernp = acids ale affivet); adn Oia ae en ne ee Me tet cent of te vn, as ine erat acsociations have 10 be formed. The building speed ofthe ith sae 230 tiok up 2 workers. Let the minimum building speed between beth "Ao mae att nae the resultant building speed x. eA ree pave to pat the maximem vales possible ofthe sum of building epeeds of N aasociations if y if you make ciaions opti soos algorithm /maximum-sum-of-bullding-specd-00ab8226/ oo am: H-include estdioh> void merge - Sent Gant ind; wid emerge -armay Cink lots tot yak annioo)s void maiN()§ int Los Print} (rn Eaten number god ("a 20); pint ("Enter Chements.wi"); Agizojicn*t) Sean (id ®, Sean), -SatCome!); print ("vn Sostect Data 2"), fom (izorien;i +t Print "4.34" aC? x 7 menge- Somntint Lint h)S int oy 7m debt mia, merge. Som); meg. sordt Uwe) ey Ms eee! om “tw cm — it); Co Elemeals anys RPP Sh nite See ae aN 218C1202 - DATASTRUCTURES, ny has just taken a shower. Now she is going to uy some gifs for her relatives, Bt the prob Lite pig Be ae See ee cease eet Ni eee eel ti hich consists of letters ('L''R',U','D') el for her, A set of instructions is string staying at point (0, 0) on the infinite plane. She consistently ful) j Jet's assume that Benny is ime that now she is staying at point (X, Y). Then depending on wi by Mike. Let's Je moves in some direction: For simpli the instructions written the current instruction from (X, ¥) moves to point (X, ¥ = 1) from (X,Y) moves to point (X, ¥ + 1) from (X, Y) moves to point (X - 1, Y) from (X, Y) moves to point (X + 1, Y) a >. now. Initially, all points are snowy. But if Benny have already me this point becomes icy (because Benny has just taken a shower). Every time, whe points she slips and falls down. “The weather is cold because it's winter some point at any tin makes a step into icy ‘you are given string S which denotes a set of instructions for Benny. ‘Your task is to calculate how r she will fall down, .e-problems/algor h.com/practice/algorithms/sorting/quick-sort/pr: odegiis-marcheas\-3/ Program: pncludec skdio > wd quecksoxt Cint £ a int ut int fail) iat i}, kem Ps pivot; vb Chout éhaxt) 24SC1202 - DATA STRUCTURES \ rth and The Meteorites, oma time, the Earth was a flat rectangular landmass. And there was no i vemorites falling from out of space. Wherever they fell on the planet, a riv tions (North, East, West, South), till the waters reached the edge of th eless in this question? ‘yn. hackerearth.com/practice/algorithms/sorting/quick-sort/practice -ms/algorithny/earthandthemetcorites-quallifier2/ am: tt inctudes skaio W> Void quicksord (rat Fa rink bout, int (ask) © int nde temp plus Nb ( foukc last) : Puot= bout; i= bisa; J= Laat; ewhile (124) z udkile (otide =atpivot)) vst a bile (ot) > =C rivet) ey vb eg) : dem pai) ali)= oC]; acs} = temp; 4 samp ali one atpiuct); Ol piyal Je temp; Biss nen

You might also like