3 views

Uploaded by Pooja Sinha

some c questions

save

You are on page 1of 8

CS41 - Design and Analysis of Algorithm Assignment Questions Unit – II 1. What is divide and conquer strategy? 2. Give the complexity of divide and conquer algorithm in recurrence form? 3. Define Binary search. 4. Describe the computing time of binary search for successful searches. 5. Describe the computing time of binary search for unsuccessful searches. 6. Write the straight forward algorithm to find max and min elements in array of ‘n’ elements. 7. Design an algorithm to find the position of maximum element among N numbers using divide and conquer. 8. Design an algorithm to find the minimum element among N numbers using divide and conquer. 9. Define merge sort. 10. Write algorithm for merge sort. 11. Write algorithm for merging two sorted arrays. 12. Give the computing time of merge sort in recurrence relation. 13. Write the greedy method control abstraction for the subset paradigm. 14. What is container loading problem? 15. State the Knapsack problem using Greedy approach.

for (int i=2. T (n) =T (1) If n > 1. Algorithm to find min and max element: void straightmaxmin (a[]. max = min = a [1]. It works by comparing a search key K with the arrays middle element A[m]. u (n) =∑kj=1 h (bj) and h (n) = f (n)/nlogba. 3. i++) { . then T (n) = a T (n/a) + f (n) By using substitution method. Computing time of binary search for unsuccessful searches: È (log n) – Best case . Worst case 6. 4. Complexity of divide and conquer algorithm in recurrence form: Complexity of Divide and Conquer is given by. Average case . min.Keys: 1. i<=n. T (n) = nlogba [T (1) + u (n)] where. If n=1. if they match the algorithm stops. Binary search: Binary search is a remarkably efficient algorithm for searching in a sorted array. Computing time of binary search for successful searches: È (1) . int n) { int max.Best case È (log n) – Average case È (log n) – Worst case 5. otherwise the same operation is repeated recursively for the first half of the array if K < A[m] and the second half if K > A[m]. Divide and conquer strategy: divide problem into sub problems recursively solve sub problems combine solutions to sub problems to get solution to original problem 2.

pos2=pos1. max). } else max = a [i]. max1). } if (a [i] < min) { min = a [i]. pos1=j. Parameters i & j are integers. pos2. int j) //a[ 1 : n ] is a global array. j. } else if (i == j-1) { if (a [i] < a [j]) { max = a [j]. } } 7. . if (max < max1) { max = max1. maxpos ( mid+1. //max position is i. } else { int mid = (i + j)/2. 1 <= i <= j <= n. pos1=pos2. Algorithm to find the position of maximum element among N numbers using divide and conquer: void maxposition(int i. max1. pos1=i. { int pos1. if (i == j) { max = a [i].if (a [i] > max) { max = a [i].max. max1=max. maxpos (i. mid.

if (min > min1) { min = min1. min1. max1). pos1=j. } } } 8. minpos ( mid+1. Algorithm to find the minimum element among N numbers using divide and conquer: void minposition(int i. } else . j. pos2=pos1. min1=min. int j) //a[ 1 : n ] is a global array. pos1=pos2. } else { int mid = (i + j)/2. } else if (i == j-1) { if (a [i] > a [j]) { min = a [j].min.//max position is pos1. { int pos1. //min position is pos1. mid. } else min = a [i]. } else { //max position is pos1. pos2. Parameters i & j are integers. minpos (i. max). if (i == j) { min = a [i]. //min position is i. pos1=i. 1 <= i <= j <= n.

.. int high) /* a [low: high] is a global array to be sorted. int mid = (low+high)/2. } } . high). In this case the list is already Sorted */ { If (low<high) {// If there are more than one element // Divide P into sub problems. // Combine the solutions Merge (low. } } } 9.n-1] sorting each of them recursively and then merging the two smaller sorted arrays into a single sorted one. Algorithm for merge sort: void MergeSort (int low. high). mid.n-1] by dividing it into two halves a[0. Small (p) is true if there is only one element to sort.(n/2)1] and A[n/2.{ //min position is pos1. MergeSort (mid +1. 10. mid). // Solve the sub problems MergeSort (low.. Merge Sort: Merge sort sorts a given array A[0. // Find where to split the set.

int mid. k. Algorithm to merge two sorted array: void Merge (int low. i++. i++. } } else { for ( k=h. int high) /* a[low: high] is a global array containing two sorted subsets in a[low: high] and in a [mid +1: high]. k++) a[k] = b[k]. i= low. b [] is an auxiliary global array. j= mid+1.11. k<=mid. k++) { b[i] = a[k]. } else { b[i] = a[j]. k<=high. k<=high. j++. */ { int h = low. } } { for (k=low. The goal is to merge these two sets into a single set residing in a a[low: high]. }} . } } if ( h > mid) { for ( k=j. while (( h<= mid) && (j<=high)) { if (a[h] <= a[j]) { b[i] = a[h]. k++) { b[i]=a[k]. h++.

then T (n) = 2 T (n/2) + f (n) Two sub problems of size n/2 each that are solved recursively f (n) time to do the merge T (n) = O (n log n) 13. Different containers may have different weights. i<=n. int n) { SolType solution = EMPTY. i++) { Type x= Select (a). Let wi be the ith container. Container loading problem: A large ship is loaded with cargos. for (int i=1. Greedy method control abstraction for the subset paradigm: SolType Greedy (Type a[]. 1 ≤ i ≤ n. Feasible solution n ∑ wixi ≤ c and xiå {0.1} . if ( Feasible (solution. Computing time of merge sort in recurrence relation: Let T (n) be worst case time on a sequence of n keys If n = 1. then T (n) = f (1) (constant) If n > 1.12. The cargo capacity of the ship is c. } } return solution. 1 ≤i ≤n i=1 x=1 –> container is to be loaded x= 0 -> container is not to be loaded . x). x)) { solution = Union (solution. The cargo is containerized. We have to load the ship with the maximum number of containers. } 14.

. 1 ≤ i ≤ n.Every feasible solution that maximizes the objective function. Knapsack Problem – Greedy Approach. Maximize ∑l ≤ i ≤ n pixi m Subject to ∑l ≤ i ≤n wixi ≤ and 0 ≤ xi ≤ 1. 15.Objective function Maximize n ∑ xi i=1 Optimal Solution .

- 10-binarysearchUploaded byapi-3799621
- DaaUploaded byAnup Shanbhag
- Data Structure for JdkUploaded bysanjeevr811
- Algos syllbUploaded bychaitu215
- ADBMS-TypicalQueryOptimizerUploaded byGaurav Kispotta
- WL013112001202Uploaded byAnonymous 7VPPkWS8O
- Quick SortUploaded bySurya Nair
- Cheat SheetUploaded bysandip
- Complexity of AlgorithmsUploaded byssMShahzadAhmedss
- 03 FusionUploaded byWalter Cabrera
- merge sortUploaded byArnex Fesariton
- Lec6Uploaded byAanand Rishabh Daga
- scrweibdUploaded bySridhar Tamil
- Data Structure Feb18Uploaded bynisha sharma
- Theory of algorithmsUploaded byiv727
- C QuestionsUploaded bySreekar Kokkonda
- 20150929 Edit Distance Computational ComplexityUploaded byckrich_991960
- Business Warehouse - Data Staging and ExtractionUploaded byClaudio Piñevsky
- lec1Uploaded byUmair Mughal
- Algorithmic Aspects of Wireless Sensor NetworksUploaded bysansiet
- AdaUploaded byapi-3708630
- may 2010 c and dsUploaded bySanethel Maan
- chapter 7 tocUploaded byYubraj Chaudhary
- fogaras05ppr_ext.pdfUploaded byShruti Bansal
- 23 SlideUploaded byHoàng Vũ
- SetsUploaded bygurusodhii
- timer-wheel.pdfUploaded bySarah Coleman
- SortingUploaded byArun Munvalli
- Chapter09.2 (1)Uploaded byitztru
- B.Sc-IT III SemUploaded byapi-3705029

- 10-binarysearchUploaded byapi-3799621
- DaaUploaded byAnup Shanbhag
- Data Structure for JdkUploaded bysanjeevr811
- Algos syllbUploaded bychaitu215
- ADBMS-TypicalQueryOptimizerUploaded byGaurav Kispotta
- WL013112001202Uploaded byAnonymous 7VPPkWS8O
- Quick SortUploaded bySurya Nair
- Cheat SheetUploaded bysandip
- Complexity of AlgorithmsUploaded byssMShahzadAhmedss
- 03 FusionUploaded byWalter Cabrera
- merge sortUploaded byArnex Fesariton
- Lec6Uploaded byAanand Rishabh Daga
- scrweibdUploaded bySridhar Tamil
- Data Structure Feb18Uploaded bynisha sharma
- Theory of algorithmsUploaded byiv727
- C QuestionsUploaded bySreekar Kokkonda
- 20150929 Edit Distance Computational ComplexityUploaded byckrich_991960
- Business Warehouse - Data Staging and ExtractionUploaded byClaudio Piñevsky
- lec1Uploaded byUmair Mughal
- Algorithmic Aspects of Wireless Sensor NetworksUploaded bysansiet
- AdaUploaded byapi-3708630
- may 2010 c and dsUploaded bySanethel Maan
- chapter 7 tocUploaded byYubraj Chaudhary
- fogaras05ppr_ext.pdfUploaded byShruti Bansal
- 23 SlideUploaded byHoàng Vũ
- SetsUploaded bygurusodhii
- timer-wheel.pdfUploaded bySarah Coleman
- SortingUploaded byArun Munvalli
- Chapter09.2 (1)Uploaded byitztru
- B.Sc-IT III SemUploaded byapi-3705029
- Lauree triennali 0318 - esiti.pdfUploaded byLorenzo Spongano
- BATAS WILAYAH NEGARA KESATUAN REPUBLIK INDONESIA.docxUploaded byFransisca Julia
- Detalles Contstructivos en PlanosUploaded by12345aatgd
- ProgramaUploaded byMelodescargas Programas Gratuitos
- soal crdas cermatUploaded byBunga Cantia
- Re 10 Lab 035 001 Microbiologia IIUploaded byJoseph Quispe
- a - Intensidade do Treino pela FC de Reserva.docxUploaded byRaul Sawitzki
- Moment ključ.pdfUploaded byDražen Horvat
- 01. Politica de Compensacion y BeneficiosUploaded bydaniel
- cesar oliva.pdfUploaded byJesi Leyria
- Plan de Estudio_7º y 8º básico (1).pdfUploaded byDarleen Velásquez Rodríguez
- 1 Conceptos Básicos- Conceptos Básicos de refrigeracionUploaded byGuillermoAlejandroCajal
- 239429787-Pronzato-Alessandro-Palabra-de-Dios-Ciclo-a.pdfUploaded byCARLOS BÁRZOLA
- Acosta,J. Sempere (2017) Resumenes Ext ProExplo 2017 Metalogénesis.-horizonte MineroUploaded byWilliamsRafaelMataRimac
- Te1 Kelompok 3 BigdataUploaded byFredy Marpaung
- FIRDAUS MAULANA IQBAL (FISIKA).pptxUploaded bymunazilatulchasanah
- TheColdDrinks.docxUploaded bypmp aspirant
- Nouveau Document Microsoft WordUploaded bybahim02 ouldoumer
- kaka2.txtUploaded bymailrahul1994
- IDENTIFICACIÓN-Y-ANÁLISIS-DE-PROCESOS.docxUploaded byJoseph Hidalgo
- Abrir acceso por red a Xampp.pdfUploaded byBLEIKER
- Metode Numerik 4 Metode Bagi 2Uploaded byZizah Deci
- asma.docxUploaded byNurfitri Firman
- Guía de ClasesUploaded byanon_612246041
- bag 3 modul 10Uploaded bycaptainkidd
- Aula Introdutória - 5 Solas Da Reforma ProtestanteUploaded byArlei Veloso
- BANCO PREGUNTAS 2013 ENF..pdfUploaded byDaniel Oleas
- ScheduleUploaded byChristine Aev Olasa
- Diit Penderita Penyakit KardiovaskulerUploaded byPuspita Damayanti
- Wadsa Forest Division Gadchiroli Forest Circle For the Year 2012-13 to 2021-22 - Vol 1.Uploaded byeaposztrof
- world_physical_2011_nov.pdfUploaded byPooja Sinha
- ciml-v0_8-all.pdfUploaded byPooja Sinha
- CSP_2015_eng.pdfUploaded byPooja Sinha
- world.pdfUploaded byPooja Sinha
- EconomyUploaded byPooja Sinha
- solve_chemistry_question_paper_2014_cbse_board_class_xii.pdfUploaded byPooja Sinha
- intro_analysis.pdfUploaded byPooja Sinha
- CSP_2015_eng.pdfUploaded byPooja Sinha
- chemistry_1_x11_2012.pdfUploaded byPooja Sinha
- world.pdfUploaded byPooja Sinha
- ncert4Uploaded byPooja Sinha
- Math_Code_No_1.pdfUploaded byPooja Sinha
- Math_Code_No_1.pdfUploaded byPooja Sinha
- country_project.pdfUploaded byPooja Sinha
- result.pdfUploaded byPooja Sinha
- world.pdfUploaded byPooja Sinha
- CS_SET_2_GATE_2015.pdfUploaded byPooja Sinha
- CS2013.pdfkey.pdfUploaded byPooja Sinha
- uge01Uploaded byPooja Sinha
- A1LaUploaded byPooja Sinha
- 0204Uploaded byPooja Sinha
- ArchimedeanUploaded byPooja Sinha
- CS_SET_2_GATE_2015Uploaded byPooja Sinha
- 01 StreamsUploaded byPooja Sinha
- CS02_20142.pdfUploaded byPooja Sinha
- CS02_20142.pdfUploaded byPooja Sinha
- CS03_20143.pdfUploaded byPooja Sinha
- af1Uploaded byPooja Sinha
- yunUploaded byPooja Sinha
- Class Notes 5Uploaded byPooja Sinha

- world_physical_2011_nov.pdfUploaded byPooja Sinha
- ciml-v0_8-all.pdfUploaded byPooja Sinha
- CSP_2015_eng.pdfUploaded byPooja Sinha
- world.pdfUploaded byPooja Sinha
- EconomyUploaded byPooja Sinha
- solve_chemistry_question_paper_2014_cbse_board_class_xii.pdfUploaded byPooja Sinha
- intro_analysis.pdfUploaded byPooja Sinha
- CSP_2015_eng.pdfUploaded byPooja Sinha
- chemistry_1_x11_2012.pdfUploaded byPooja Sinha
- world.pdfUploaded byPooja Sinha
- ncert4Uploaded byPooja Sinha
- Math_Code_No_1.pdfUploaded byPooja Sinha
- Math_Code_No_1.pdfUploaded byPooja Sinha
- country_project.pdfUploaded byPooja Sinha
- result.pdfUploaded byPooja Sinha
- world.pdfUploaded byPooja Sinha
- CS_SET_2_GATE_2015.pdfUploaded byPooja Sinha
- CS2013.pdfkey.pdfUploaded byPooja Sinha
- uge01Uploaded byPooja Sinha
- A1LaUploaded byPooja Sinha
- 0204Uploaded byPooja Sinha
- ArchimedeanUploaded byPooja Sinha
- CS_SET_2_GATE_2015Uploaded byPooja Sinha
- 01 StreamsUploaded byPooja Sinha
- CS02_20142.pdfUploaded byPooja Sinha
- CS02_20142.pdfUploaded byPooja Sinha
- CS03_20143.pdfUploaded byPooja Sinha
- af1Uploaded byPooja Sinha
- yunUploaded byPooja Sinha
- Class Notes 5Uploaded byPooja Sinha