SlideShare una empresa de 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.

Más contenido relacionado

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

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 notaluavi
 
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 SolutionHazrat Bilal
 
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 assignmentZenith SVG
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdfsowmya koneru
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solutionAzhar Javed
 
Operating system labs
Operating system labsOperating system labs
Operating system labsbhaktisagar4
 
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.2020vrgokila
 
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.pdfAshutoshprasad27
 
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.docxAshutoshprasad27
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using CBilal Mirza
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shortingargusacademy
 
Basic C Programming Lab Practice
Basic C Programming Lab PracticeBasic C Programming Lab Practice
Basic C Programming Lab PracticeMahmud Hasan Tanvir
 
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.pdfrushabhshah600
 

Similar 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
 

Más 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.docxlcarolyn
 
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.docxlcarolyn
 
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-.docxlcarolyn
 
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.docxlcarolyn
 
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.docxlcarolyn
 
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.docxlcarolyn
 
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.docxlcarolyn
 

Más 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

Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6Vanessa Camilleri
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 

Último (20)

Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 

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.