SlideShare uma empresa Scribd logo
1 de 2
2 Write a program to implement the selection algorithm.
A) Prove that merging two sorted arrays of N items requires at least 2N-1 comparisons.
Solution
2 Write a program to implement the selection algorithm.
#include <stdio.h>
int main()
{
int data[100],i,n,steps,temp;
printf("Enter the number of elements to be sorted: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("%d. Enter element: ",i+1);
scanf("%d",&data[i]);
}
for(steps=0;steps<n;++steps)
for(i=steps+1;i<n;++i)
{
if(data[steps]>data[i])
{
temp=data[steps];
data[steps]=data[i];
data[i]=temp;
}
}
printf("In ascending order: ");
for(i=0;i<n;++i)
printf("%d ",data[i]);
return 0;
}
A) Prove that merging two sorted arrays of N items requires at least 2N-1 comparisons.
The observation about mergesort is that no matter suppose input array looks like, mergesort will
divide
the array recursively into the same two sorted array. The way the input influences the way the
algorithm works
in is the merge phase of the algorithm. A merge of two N-element sorted arrays requires at least
N comparisons (this happens if one array's elements are all less than the other array's smallest
element) and at most 2N-1 comparisons. In any case, merge requires 2N assignments.
So, merge requires Theta(N) time regardless of the input.
This implies that mergesort takes when marging two sorted arrays of N item requires at least 2N-
1 comparisons.

Mais conteúdo relacionado

Semelhante a 2 Write a program to implement the selection algorithm- A) Prove that.docx

Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
rushabhshah600
 

Semelhante a 2 Write a program to implement the selection algorithm- A) Prove that.docx (20)

Write a program to check a given number is prime or not
Write a program to check a given number is prime or notWrite a program to check a given number is prime or not
Write a program to check a given number is prime or not
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
 
C programming codes for the class assignment
C programming codes for the class assignmentC programming codes for the class assignment
C programming codes for the class assignment
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Operating system labs
Operating system labsOperating system labs
Operating system labs
 
insertion sort.pptx
insertion sort.pptxinsertion sort.pptx
insertion sort.pptx
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
array.ppt
array.pptarray.ppt
array.ppt
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
C Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossainC Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossain
 
Arrays
ArraysArrays
Arrays
 
Ds
DsDs
Ds
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
Basic C Programming Lab Practice
Basic C Programming Lab PracticeBasic C Programming Lab Practice
Basic C Programming Lab Practice
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
 

Mais de lcarolyn

1Experimental Chemistry Data Analysis 1- How do you know when to name.docx
1Experimental Chemistry Data Analysis 1- How do you know when to name.docx1Experimental Chemistry Data Analysis 1- How do you know when to name.docx
1Experimental Chemistry Data Analysis 1- How do you know when to name.docx
lcarolyn
 
18) Explain why oil and water do not mix- A) Water is not able to ioni.docx
18) Explain why oil and water do not mix- A) Water is not able to ioni.docx18) Explain why oil and water do not mix- A) Water is not able to ioni.docx
18) Explain why oil and water do not mix- A) Water is not able to ioni.docx
lcarolyn
 
2- Does evolution violate the second law of thermodynamics- Why or why.docx
2- Does evolution violate the second law of thermodynamics- Why or why.docx2- Does evolution violate the second law of thermodynamics- Why or why.docx
2- Does evolution violate the second law of thermodynamics- Why or why.docx
lcarolyn
 
2- Compare teh payment of cash divididends- stock dividends and purcha.docx
2- Compare teh payment of cash divididends- stock dividends and purcha.docx2- Compare teh payment of cash divididends- stock dividends and purcha.docx
2- Compare teh payment of cash divididends- stock dividends and purcha.docx
lcarolyn
 

Mais de lcarolyn (7)

1Experimental Chemistry Data Analysis 1- How do you know when to name.docx
1Experimental Chemistry Data Analysis 1- How do you know when to name.docx1Experimental Chemistry Data Analysis 1- How do you know when to name.docx
1Experimental Chemistry Data Analysis 1- How do you know when to name.docx
 
18) Explain why oil and water do not mix- A) Water is not able to ioni.docx
18) Explain why oil and water do not mix- A) Water is not able to ioni.docx18) Explain why oil and water do not mix- A) Water is not able to ioni.docx
18) Explain why oil and water do not mix- A) Water is not able to ioni.docx
 
17-Calcium sulfate has a soluble form in water- formed according to-.docx
17-Calcium sulfate has a soluble form in water- formed according to-.docx17-Calcium sulfate has a soluble form in water- formed according to-.docx
17-Calcium sulfate has a soluble form in water- formed according to-.docx
 
2- Does evolution violate the second law of thermodynamics- Why or why.docx
2- Does evolution violate the second law of thermodynamics- Why or why.docx2- Does evolution violate the second law of thermodynamics- Why or why.docx
2- Does evolution violate the second law of thermodynamics- Why or why.docx
 
2- Discuss the advantages and disadvantages of using the following com.docx
2- Discuss the advantages and disadvantages of using the following com.docx2- Discuss the advantages and disadvantages of using the following com.docx
2- Discuss the advantages and disadvantages of using the following com.docx
 
2- Create a multi - step Income Statement- Statement of Retained Earni.docx
2- Create a multi - step Income Statement- Statement of Retained Earni.docx2- Create a multi - step Income Statement- Statement of Retained Earni.docx
2- Create a multi - step Income Statement- Statement of Retained Earni.docx
 
2- Compare teh payment of cash divididends- stock dividends and purcha.docx
2- Compare teh payment of cash divididends- stock dividends and purcha.docx2- Compare teh payment of cash divididends- stock dividends and purcha.docx
2- Compare teh payment of cash divididends- stock dividends and purcha.docx
 

Último

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 

Último (20)

Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 

2 Write a program to implement the selection algorithm- A) Prove that.docx

  • 1. 2 Write a program to implement the selection algorithm. A) Prove that merging two sorted arrays of N items requires at least 2N-1 comparisons. Solution 2 Write a program to implement the selection algorithm. #include <stdio.h> int main() { int data[100],i,n,steps,temp; printf("Enter the number of elements to be sorted: "); scanf("%d",&n); for(i=0;i<n;++i) { printf("%d. Enter element: ",i+1); scanf("%d",&data[i]); } for(steps=0;steps<n;++steps) for(i=steps+1;i<n;++i) { if(data[steps]>data[i]) { temp=data[steps]; data[steps]=data[i]; data[i]=temp; } } printf("In ascending order: "); for(i=0;i<n;++i) printf("%d ",data[i]); return 0; } A) Prove that merging two sorted arrays of N items requires at least 2N-1 comparisons.
  • 2. The observation about mergesort is that no matter suppose input array looks like, mergesort will divide the array recursively into the same two sorted array. The way the input influences the way the algorithm works in is the merge phase of the algorithm. A merge of two N-element sorted arrays requires at least N comparisons (this happens if one array's elements are all less than the other array's smallest element) and at most 2N-1 comparisons. In any case, merge requires 2N assignments. So, merge requires Theta(N) time regardless of the input. This implies that mergesort takes when marging two sorted arrays of N item requires at least 2N- 1 comparisons.