Workbook

You might also like

Download as pdf
Download as pdf
You are on page 1of 22
21SC1202 - DATA STRUCTURES JARTMENT OF BASIC ENGINEERING SCIENCE ~ | SUBJECT CODE: 2151202 FED : DEPI DATA STRUCTURES WORKBOOK WEEK -1 Lab Session: Date of the Session: _/_/__ Time of the Session: __to de and Conquer Algorithm Sorting swapping items in place Partitioning of the array. 1 In Indian battalion 35 - NCC Brigadier Vishal wants to sort his troop’s corps in ascending order according to the corps height. So, help Brg. Vishal to arrange corps accordingly. (Imagine corps height as elements in array). Analyze the time complexity(O(n log n)) Hint: Insertion sort helps to sort array in best time complexity Program Atinelod ial tagertonSord (int 7, in! iR(aljeatit te alia tat mara) l int i a woo MY y prinff ("enker Size! yy scan? ("Ad kn ) ink atny prinke(" ‘nk cvey elements - "ys foc(ic =o) lens ie) scan ("dK Ci ‘Ty ‘insertion Serf (na, Yeutor talizo, CA y Print (sy 4 : ali); Fetury o 21$C1202 - DATA STRUCTURES a tt apply shell sort.. The first line contains an integer, N, th na list of N array element est tenes Bas. at etcond line contains N space-separated integers. Print the array as a row of space tems a — {integers in each iteration. Program := include .¢ gop >=! 5 gop = gepl2) ' foiliegap;!=a0psd ~J-ger) f iP(alj) void insertonSert (ink all,inkn) eee ee int i,j, beky fot licoy ins ite) § for (set jjreys--) f i#(alileafiny f +-alil; aljj-alj-3; afj-iJ= t; j tox (kk=05 keny kes) Priel? ("zd "a Cea); Prinke("\n"y; J 2 eerste int main() i int ais : scant (" 4d", £0) ink alnd; tov (Ie0, inj ia) Scant ("}d", xa (iI); insertion Set] (ayn) Yeh 0; Input 0 eae) eg Output ae Reha a. 24 99 age — : 24 49 aria oe “10-4 s 4 nag ee to Fay W499 de 9 t oe ie lees 5 Me 7 2 pre i mh al, ie a oo. 9 wy 21SC1202 - DATA STRUCTURES 3. Merge Sort Given alist of N array clement apply Merge sort, Merge Sor isa Divide and Conquer algorithm. Ie divides inp corm two halves, cals itself forthe two halves and then merges the two sorted halves, The first line cosine ary Tyee, Nhe numberof elements in Array. The second Tine contains N space-separaed integers. rin the gray as a row of space-separated integers in each iteration. etps://vww.hackerrank.com/contests/17¢es1102/challenges/merge-sort-6 Prograt stinclude void merge -sovt (ink, int); void merge -arvay (int int, int int y int avy [te00] 5 ink main() ; int iyny sant ("xd"), gn); for (i=1; i Lab Session: Date of the Session: _/_/ 21SC1202 - DATA STRUCTURES. Prerequisite: Divide and Conquer Algorithm Sorting swapping items in place Partitioning of the array. Pre Lab Tash Week-2 Time of the Session: to 1. Chris and Scarlett were playing a block sorting game where Scarlett chal blocks which arranged in random order. And Scarlett puts a restriction that one ae io som the ‘median and last blocks to sort, and after sorting one block with reference to other block, eet ‘must choose another block as the reference not the same block (random pivot). Now, Chris we eee to sort the blocks, He wanted to sort them in a least time. Help him with the least time ae algorithm. complexity sorting Program := include void quickSett( int ov [ioe }, int, int}; ink partition (int avy [tov], int int); int main) i int avy [102] ,n, 15 prinld scant ("/d" gn); Puilizozicn; ist) scanF("4d" geali]); quickSerd (arr ,n-1, ie prinkp (1 \nSerted array is \n\n"); tor (i= 9; len; Vite) Pritt ("Lyd" ar Ci) re ' mn 0, 2 LH void guick Sort Cink arr CI, int foes, inf hgh) oS iat) iE (dow chigh) | = partition (arvlow, high)’ gquickSeck (arr, low, j1) quickSwrt (ars +rngh), q . 5 int pavhhon Cint ary ftoo] pint loco, iat hugh ) int pivot , jy term, ik; if ( Now < high) pivot=low+tf i =low, Jehighy while (i prvCpivetd ) iv iRlEj) £ ernp = or Ci] aw [iJsonlidy an [j] = temp; 3 Hemp = arr [pivot Vy arr [pivet Jean [J] ani Jzkmp5 Been iE A 21SC1202 - DATA STRUCTURES sare playing a game, rules of the game are as follows: Each member will be assi signed with A group of 9 friend yes like c.g: 7, 6, 10, 5, 9, 2, 1, 15, 7. Now they will be sorted in ascendii ling order in ‘number and the sequence £0 hh a way that tallest one will be sor ition ofthe given sequence and print the order of indi ted first. Now your task is to find the order of indi of in ; es at the endoftheitersion ns mil int ated bs sintt ("enter siza:")) ean ("Ld KOY ink a[2][]; for(ieojicn; 144) f afoylil=7 Bibenk A Lbbbhe ; seank "rd", ka CTL); pleny #4) for (jet; de=Ui--)E iP (oOTLi7 Void shells e (ind of], ind > ind inj gop, tok 5 or ( gop nj, 5 9P>=15 gopzgop]2) i tm ( gop 3 fenzian) {-99p) UC] egeps P 70 fi; (J=6 Ci gery; oly Pat; 4 i fal keorken; koe) Praha" a : a Print? O\n tay ind main() 4 ind n, ty secant ("pd KA); Yad a [nJ 5 Sorlisoj ienj lee) seank (nd, kal?]) j shall $4 (a) n); Tehin 05 5 put 5 (Oe) oe, yo} eee Svtput § 910 2 6 e £7 4 6 876 49 19 +8 6 4 21$C1202 - DATA STRUCTURES . Insertion Sort / / sar are given an artay AA of size NN. Sort the array using Insertion Sort and print the sorted ara void. inSertion Sort (int a (J, it m) int i,j, ty fov(iztpiensi+t) fo(jeljd>=tsde-) if (aCj] alj-rJ alj-]=b; int ripe ky Scank ("1d Kt); Foo (ke ti KE; kee) i scant ("/d" kn); ink olny; Foy(iz0; eng i44) Stank (Ad, wali I): inserhion Sort (@,n); 27 for(i 0; (ony 14#) prt ("7d ", oli); t sefum 0; we ° cS 4 21SC1202 - DATA STRUCTURES 4, Quick sort 1 — Partition ‘The previous challenges covered Insertion Sort, which is a simple and intuiti i time’ In these next few challenges, we're covering a divide-and-conquer Resi cuca with a running ar partition Sort). This challenge is a modified version of the algorithm that onl Quick sort (also known implemented as follows: ly addresses partitioning, I i hitps://www-hackerrank.com/challenges/quicksort1/problem include alPy) a IF (i fen Bee Blt ~.. gq 7 a (ooo we ee ee le ba —— fH 32 45 2 ug Ge Ee aq ea: — — Mee ee a hoo 32 4S Wt ge Ga aq 84 a oe oo ee | ae | au. 24 a Som te oe [t Fe au 8 nN 4s) 44 c+{ pte od a as ua «| a ne swap us ua ¢# aa | 34 8 a4 us 4g 6F «| . ol Bu Ge ud ee cee Bt ay 8} os ey 34 a7 a4 8F 34 at st st 21SC1202 - DATA STRUCTURES 1._ Quick Sort ‘You are given an array AA of size NN. Sort the array using Quick Sort and print the sorted array. www. codechef.com/DSCA2019/problems/NSECDS06/ stinclude void guick Sort (int om [ww int ,int); int partition ( int am C100), init ); int main () i inh aw [t00I, n, i; sean? ("kd", any for (i=0; ior iar) scant (“d", wary (iD; quickSort (am, 0,-1; for (1-0; fen site) printk ("Ad *, aw Lid) Felurn 0; 4 void quickseit Cink owl7 tnt low int high ) i int LU 12 (toe av Cptvetd) iy if (ict erp = om (iJ; on (i=orr (j 5 av [7 I= Kemp; 4 3 temp = ovr [pivel 1; bw [plus] = aw (il; : an{jJ= herp: vehun j;

You might also like