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

Sorting and Searching

Uploaded by

G Sathish
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
18 views

Sorting and Searching

Uploaded by

G Sathish
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 22
— Z2ttolet ont TD DIvide ¢ CONOVER METHODS Divide the problem based Tat dzvo or move Subproblems of tho 40Ma sera type socunsivety und thi become Aimplo enough t be goed Aboctory thon Solve each stb preblam aeparotaly and pfhaliy corniina Hig Sosulk Lad swbprodem tv Five o zoudion & the or ginas Probam - Divide Tho pwbiem Eko ona Ox Mp AubprOb\M. Subpro bans by goiving aha soauxsivaly combina. © Tho gouusen to “ihe sudproblons anko gpwefon to sho exiginal peoblam. Texoblem P. Divide phase ¥ 2 Bi Po < 1 >— Ata ibya) 7 Pai\ (P22) {Par [P32] P33) (Pad) cong i) Ba [3 eae ~ ae a Gonquor phase - Sn ~ aint lasts 1a \g2 io) Z 2 (54 te) abt 43) 3) 7 a re ro oe c& (| Ge) ~ : oa a ps CNL) Ning Bs sa 7a 7 Raa, Qs 32 Algosditin tox civide ¢ conquon oe Road the problem P ef sizon o “Nie P hte ™m suppsoblom ef $P,,Pa,.-. Pmy whore m

stop 1 ake ie alamont to be Ferached oS FY: -okood tho 4Bak etomant Pp Aha sequence ; Staten: with bay Th tho ckre Compdoe on ont yo lamar “38h ON onsfrws. t00r Cov) poordss- eps 24 & no maketh foun und than tho cay ch ® pot present 7n “he sequon cy - End Voor gonsuth » 3 ome e Be kay? \ 8fo) POI we af) pte) 98) ale) ACT me eye .@ 2 ls b ae wee AE SRO 5, 44 @ is b qo lob as “ee 2 2 4s 52 (jo fee Oo ero) G ts (be) 92 1b aS I 2 po 4s st, Keay value Jeompax ght 4 1 tte) ee ec omar: Pxogram pov Linear £eaxch #Ynctudo < xidlo-h > ~ veld rath ©> ‘int a LtooJ 51,0, Low , toy | doc =z-l} Patndh (° Ente, tho number Bf data clomant fn Uist); scan C7", Bnd; PHN Mentor tho data elomont ore. by ane; for (F205 %eN 744) : sean} (4. saci): } PAL, C"Enty, Ae data glamont to be er Scan} Od" Leroy)? . Cito; Pens ies) oe Ay Carry = soy qa Loc =2 3 , a bee break ; H toons do id”, 1 fies) & ydlfound- at casfon a (toc), preg oe c - Doc Ds os ths printf dato. aac rand. 4 rey: $ olp tnios Tho rumba of, dato alomant 3 & + tale, tho data clomenk ong dy oY Utim —he dnto alomon$ tn be Row nghed 19D Found at locdtion 4_ hi Binasy Loach er Ae Algositho Co Stop |= she given gequonta 88 1. doka olomart stop 22nd the middle ctamort tte aay Sho, : wu 7 SAP A SCormpany lety eulth ho middle elomonr & key emdvaus Hun to desired value gota) Va In tho aiddle stop ths prowss auceasfuly : . ~kpee SY key emBdwows thon do seanch on Lo olka Bras Search @ oe ta ony nench ython dp Seaneh on Stop: On seasuth , y ony mate mofch AocakFon Whand the moakch ine —— a g& suceaaspud Cte jeoncuucl cue tha Jo0xch vou J Q WH that ovay nos carte’ end Binoy Lean Sequance + 52,4, WA 1G, b,90,2, AS512 ‘ Koy 252. Step 1: aa efivon ink & homens 52,540,148. sb 45125 1S, 45> Athos senting stop ae CUE ho mfddle v vue ~ (0%8) 2 ~ 1 o 8 aS ae i sa 2 8 as 52 WNT, ae © 2 ay 3 stop 32 M='S 262. : s aot Ss? 252 1 apie wh ‘ee Binclude 2 Sido. h > yord sia > Yor PY? md > § srt aciocd. 1.07 i prinnh Cente the no- of daéa starts 8 poonkg c° Enon tho data elomont ono by eno in ascending ondanr’) ; for Peo o;fens fap Scank'yd", acy) PR Cento data Liam to be searched’) SCAU (" 7.4", & ROY? . doco: beg 207 end =N-'; wits (Cbeq <= = end) FF Ceey | eat) mid (pegs endd 125 BY Very = acmiday) 2 pwine} C* mageh found § search BB Loc = mid; palaeh OVA fourel ab Combi Zl? Key, Qoe-+); on Bey = Plag +l) 4 . Cleo if Ckey 4a EmiaJ) end = mid-V ele Lo beg 2 wud; 3 is Pag = = a ~Conmh fe nok ute S 33 © Te: Enter the nurber & data lems jn die lub sb enter the Anta items one by one @ 22 5S be to be etcee) iss ss found ab Cooation p Lansestion Lect R one 4 Fhe Bionpleat Sorting algonthn. ie Lagerton Sb. ME tection Sot kb to Conlader earh element ak a time bsosts & seb op Volos by Tnsenbing Volues jep a1 Crating Soated ile. HE EE sets Cash Wen Tako Fis proper Plue fo he final Usb. This & gust Ue Plhyinty The Sovted Romonks one eon , Rags Ade of Ho Vee & unsorted elements Vhe insertion Sort Lowksts of N-/ Void For fous Pot through -} elements, Veserbion Soab Cres pip ano clomonts Sn pesition o rough e. * Comason an Bw hemonts Sindh Rok ALIS AD2I. ALA) & in mamery . % Tho itkestion Bort the A doom pry te ACY), TWAT, gph lament ADK) into its Peps Posttion in +h pers sub ores ALY, ALY ACK. spn SeanS * bela A | unocderer pub] hye Fep 2 Olen oh deat ete aie ie mga Be on frat Senpined eles Coe ted past, SAD Qhemeds g a Hew ere wig pe. faseed Pn the %3e: 20/30 ye, BY B87 t f oplnentatin Gf Souslin fot tinclude Void maint? fae ALS, 071; protf Co How meu elements]”), Seanf C/A 8); for Ciel; iza; it sang (“fA 20 Li): jnaoxtiongort (4/0) “ Soe Cray jan} itt) past 7b! RACY); Void Ingerbionsort (Ai) E- fae ATTN, iat hy Jp xr Pe goc (is; 12007 #9) ty = pci ) jel} white x4aC 5-1) 4 ACH = AC paint Z d i ngeation Sort”, ALI); Tuertion Sort € IH 11 20 20 45 er Toei ” Q Sort ha, been i!harralesl C1 Ro ols, elare sel 181th aera yy Gut vein = : VylosT 3 tngorted Array fe Syepr. } [aol 26} isle le sks | fz0 Tore fins Voda & inseny | _ nto ee Sr Sey 7 Pont / Take He second | se\zo is le| ie [afi I=! vat Pee | Co to Gin ee 202.30 han yrwert ALAT=2° , & ADS | eine eae Tee ip “ted ae ie fee els pals Ia) 2 e'acngals J52go) 15420 go 15 =ADS pUIS= 22 Atay =3° Veet ib i peels) Eee _Srep bi Acids ACSI=35 Toke PK [o_o else tls Tae) FMS pacar | tg Cormposaof | +o ALATA Srep 15 : & lial 7 a! [oo|eo a [spel ier Take CP paisa a . fo gil the position ard] prove te PrePeh pesitieo Toke ave B& be to athe Fat Ynove 7 2 pepes fee Pa Joke valet lg Cot q fo all the po aoe pepe) pewien Merge sort Merge Son is one of the best examples of Divide & Conquer algorithm, In Divide & Conquer algorithm design paradigm, we divide the problems in sub-problems recursively then solve the sub-problems. & at last combine the solutions to find the final result. ivide & Conquer algorithm has 3 steps: vide: Breaking the problem into subproblems 2. Conquer: Recursively solving the subproblems 3. Combine: Combining the solutions to get the final result > In Merge sort, we divide the array recursively in two halves, until each sub-array contains a single element, and then we merge the sub-array in a way that it results into a sorted array, > Merge() function merges two sorted sub-a array[l..nJ and arrn “1. r] ys into one, wherein it assumes that sorted. Example: Divide the unsorted array recursive! intil | element in each sub-array remains, sort ‘cursively until | element in ray ins. Th de db ob sl) GS ING Merge emerge Lastest egestas Ts! ee a #include ele #define max 10 int af11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0} int b[10]; void merging(int low, int mid, int high) { int 11,12, i for(I1 = low, 12 = mid + 1, i= low; Il <= mid && 12 <= high; i++) { iffa{l1] <= a[I2]) bfi] = a[l++]; else bfi = a[l2++]; Te while(11 <= mid) bit] = afll+4]; while(I2 <= high) bli++] = afl2+4]; for(i = low; i <= high; i++) afi] = bli}; void sort(int low, int high) { int mid ifflow void quicksort(int number|25],int first,int last) { int i,j, pivot, temp; if{firstnumber[pivor]) bs ifi Heap Sort in C is a comparison-based efficient sorting algorithm. It is based on a Binary Heap data structure. Heap is a special tree-based data structure. it is a complete binary tree. Binary Heap > Abinary heap is a complete binary tree in which the elements are stored in such a way that the parent node is greater or smaller than its children nodes. It is into two types: max-heap and mit heap. In the max-h . the parent node has a greater value than the children nodes. > Min Heap In the min heap, the parent node contai > Heapify smaller value than its children nodes. Heapily is the process to make the heap data structure using the binary tree. Itis also used when the root node ; eleted from the heap. It again rebuilds the te Which follows the condition ofthe heap. 1. Since the tree satisties Max-Heap Property, then the largest item i fored at the root node, Swap: Remove the root element and put at the end of the array (nth position) Put the item of the tree (heap) at the vacant place. 3. Remove: Reduce the size of the heap by 1. 4. Heapify: Heapify the root element so that we have the hit ‘The process is repeated until all the items of the li st clement at root. are sorted, #include // Function to swap the the position of two elements void swap(int *a, int *b) { int temp = *a: *a=*b; *b = temp: } void heapify(int arr{}, int n, int i) { ind largest among root, left child and right child int largest = i: int left oie right = 2° i+ if (left arr{largest]) largest = left: Oia

You might also like