SlideShare a Scribd company logo
1 of 9
Download to read offline
#include
using namespace std;
void InsertionSort(int arr[],int size){
int temp,j;
for(int i=0; i=0){
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
}
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
/* create temp arrays */
int L[n1], R[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
/* Merge arrays back into arr[l..r]*/
i = 0; // first subarray index
j = 0; // second subarray index
k = l; // merged subarray index
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy the remaining elements of L[], if there
are any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there
are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
void printArray(int arr[], int size){//Function to print array
int i;
for (i=0; i < size; i++)
cout << arr[i] <<" ";
cout << endl;
}
int main(){
int size = 20;
int array1[] = {17,33,88,54,96,82,12,36,69,74,79,66,32,11,1,98,76,55,3,4};
int array2[] = {20,1,19,2,18,3,17,4,16,5,15,6,14,7,13,8,12,9,11,10};
int array3[] = {220,209,198,187,176,165,154,143,132,121,110,99,88,77,66,55,44,33,22,11};
int array4[] = {25,27,23,2,7,99,96,1996,85,3,24,11,17,4,1,0,8,9,98,45};
int array5[] = {15,35,85,100,40,90,70,65,25,5,10,30,55,75,80,95,20,45,50,60};
//Output for array1
cout << "Original array 1:  ";
printArray(array1, size);
InsertionSort(array1, size);
cout << "Array 1 sorted by insertion sort:  ";
printArray(array1, size);
//CALL THE MERGE SORT HERE
//PRINT THE MERGE SORT HERE
cout << " ";
//Output for array2
cout << "Original array 2:  ";
printArray(array2, size);
mergeSort(array2,0,size);
cout << "Array 2 sorted by Merge sort:  ";
printArray(array2, size);
cout << " ";
//Output for array3
cout << "Original array 3:  ";
printArray(array3, size);
InsertionSort(array3, size);
cout << "Array 3 sorted by insertion sort:  ";
printArray(array3, size);
cout << " ";
//Output for array4
cout << "Original array 4:  ";
printArray(array4, size);
mergeSort(array4,0,size);
cout << "Array 4 sorted by Merge sort:  ";
printArray(array4, size);
cout << " ";
//Output for array5
cout << "Original array 5:  ";
printArray(array5, size);
InsertionSort(array5, size);
cout << "Array 5 sorted by insertion sort:  ";
printArray(array5, size);
cout << " ";
return 0;
}
/* sample output
Note: As sorting an already sorted array does not result good. I have changed the array2,array4
sorting to merge sorts
*/
Solution
#include
using namespace std;
void InsertionSort(int arr[],int size){
int temp,j;
for(int i=0; i=0){
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
}
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
/* create temp arrays */
int L[n1], R[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
/* Merge arrays back into arr[l..r]*/
i = 0; // first subarray index
j = 0; // second subarray index
k = l; // merged subarray index
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy the remaining elements of L[], if there
are any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there
are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
void printArray(int arr[], int size){//Function to print array
int i;
for (i=0; i < size; i++)
cout << arr[i] <<" ";
cout << endl;
}
int main(){
int size = 20;
int array1[] = {17,33,88,54,96,82,12,36,69,74,79,66,32,11,1,98,76,55,3,4};
int array2[] = {20,1,19,2,18,3,17,4,16,5,15,6,14,7,13,8,12,9,11,10};
int array3[] = {220,209,198,187,176,165,154,143,132,121,110,99,88,77,66,55,44,33,22,11};
int array4[] = {25,27,23,2,7,99,96,1996,85,3,24,11,17,4,1,0,8,9,98,45};
int array5[] = {15,35,85,100,40,90,70,65,25,5,10,30,55,75,80,95,20,45,50,60};
//Output for array1
cout << "Original array 1:  ";
printArray(array1, size);
InsertionSort(array1, size);
cout << "Array 1 sorted by insertion sort:  ";
printArray(array1, size);
//CALL THE MERGE SORT HERE
//PRINT THE MERGE SORT HERE
cout << " ";
//Output for array2
cout << "Original array 2:  ";
printArray(array2, size);
mergeSort(array2,0,size);
cout << "Array 2 sorted by Merge sort:  ";
printArray(array2, size);
cout << " ";
//Output for array3
cout << "Original array 3:  ";
printArray(array3, size);
InsertionSort(array3, size);
cout << "Array 3 sorted by insertion sort:  ";
printArray(array3, size);
cout << " ";
//Output for array4
cout << "Original array 4:  ";
printArray(array4, size);
mergeSort(array4,0,size);
cout << "Array 4 sorted by Merge sort:  ";
printArray(array4, size);
cout << " ";
//Output for array5
cout << "Original array 5:  ";
printArray(array5, size);
InsertionSort(array5, size);
cout << "Array 5 sorted by insertion sort:  ";
printArray(array5, size);
cout << " ";
return 0;
}
/* sample output
Note: As sorting an already sorted array does not result good. I have changed the array2,array4
sorting to merge sorts
*/

More Related Content

Similar to #include iostream using namespace std; void InsertionSort(int .pdf

Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdfMerge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdfmdameer02
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdfsowmya koneru
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL乐群 陈
 
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm ProblemsLeet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm ProblemsSunil Yadav
 
I have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfI have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfshreeaadithyaacellso
 
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizationsEgor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizationsEgor Bogatov
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2YOGESH SINGH
 

Similar to #include iostream using namespace std; void InsertionSort(int .pdf (20)

array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
 
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdfMerge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
 
Home workassignment01 google docs
Home workassignment01   google docsHome workassignment01   google docs
Home workassignment01 google docs
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL
 
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm ProblemsLeet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
 
week-20x
week-20xweek-20x
week-20x
 
I have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfI have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdf
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
Vcs16
Vcs16Vcs16
Vcs16
 
Array
ArrayArray
Array
 
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizationsEgor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
 
Java arrays
Java    arraysJava    arrays
Java arrays
 
Array
ArrayArray
Array
 
Qno 3 (a)
Qno 3 (a)Qno 3 (a)
Qno 3 (a)
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 

More from brijmote

You can use Network Monitor or type netstat -a in your command promp.pdf
You can use Network Monitor or type netstat -a in your command promp.pdfYou can use Network Monitor or type netstat -a in your command promp.pdf
You can use Network Monitor or type netstat -a in your command promp.pdfbrijmote
 
There are no statements given.SolutionThere are no statements .pdf
There are no statements given.SolutionThere are no statements .pdfThere are no statements given.SolutionThere are no statements .pdf
There are no statements given.SolutionThere are no statements .pdfbrijmote
 
The answer is B- Lagging strand.Lagging strand of DNA is synthesi.pdf
The answer is B- Lagging strand.Lagging strand of DNA is synthesi.pdfThe answer is B- Lagging strand.Lagging strand of DNA is synthesi.pdf
The answer is B- Lagging strand.Lagging strand of DNA is synthesi.pdfbrijmote
 
solution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdfsolution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdfbrijmote
 
Organs of the digestive tract1.Mouth-2.Tongue-3.Epiglottis-.pdf
Organs of the digestive tract1.Mouth-2.Tongue-3.Epiglottis-.pdfOrgans of the digestive tract1.Mouth-2.Tongue-3.Epiglottis-.pdf
Organs of the digestive tract1.Mouth-2.Tongue-3.Epiglottis-.pdfbrijmote
 
a. BeCl2. The Be has 2 bonds and no lone pairs, so it is linear. b..pdf
  a. BeCl2. The Be has 2 bonds and no lone pairs, so it is linear.  b..pdf  a. BeCl2. The Be has 2 bonds and no lone pairs, so it is linear.  b..pdf
a. BeCl2. The Be has 2 bonds and no lone pairs, so it is linear. b..pdfbrijmote
 
u can feed air by possible putting zinc chloride.pdf
                     u can feed air by possible putting  zinc chloride.pdf                     u can feed air by possible putting  zinc chloride.pdf
u can feed air by possible putting zinc chloride.pdfbrijmote
 
The ammonia molecule (NH3) has three pairs of ele.pdf
                     The ammonia molecule (NH3) has three pairs of ele.pdf                     The ammonia molecule (NH3) has three pairs of ele.pdf
The ammonia molecule (NH3) has three pairs of ele.pdfbrijmote
 
single S.pdf
                     single                                      S.pdf                     single                                      S.pdf
single S.pdfbrijmote
 
E)Mg2- note more electron filled in the valence.pdf
                     E)Mg2-  note more electron filled in the valence.pdf                     E)Mg2-  note more electron filled in the valence.pdf
E)Mg2- note more electron filled in the valence.pdfbrijmote
 
CH4 (methane) molecules only have london dispersi.pdf
                     CH4 (methane) molecules only have london dispersi.pdf                     CH4 (methane) molecules only have london dispersi.pdf
CH4 (methane) molecules only have london dispersi.pdfbrijmote
 
PbCl4 - lead tetra chloride PbSo4 - lead sulfate.pdf
                     PbCl4 - lead tetra chloride  PbSo4 - lead sulfate.pdf                     PbCl4 - lead tetra chloride  PbSo4 - lead sulfate.pdf
PbCl4 - lead tetra chloride PbSo4 - lead sulfate.pdfbrijmote
 
NiCl2 for sure. None of the rest are soluble in w.pdf
                     NiCl2 for sure. None of the rest are soluble in w.pdf                     NiCl2 for sure. None of the rest are soluble in w.pdf
NiCl2 for sure. None of the rest are soluble in w.pdfbrijmote
 
It is polar. That is part of the reason it mixes .pdf
                     It is polar. That is part of the reason it mixes .pdf                     It is polar. That is part of the reason it mixes .pdf
It is polar. That is part of the reason it mixes .pdfbrijmote
 
D. R-OH .pdf
                     D. R-OH                                      .pdf                     D. R-OH                                      .pdf
D. R-OH .pdfbrijmote
 
D intermolecular forces are important when the so.pdf
                     D intermolecular forces are important when the so.pdf                     D intermolecular forces are important when the so.pdf
D intermolecular forces are important when the so.pdfbrijmote
 
Do you have an image of the IR If so it would be.pdf
                     Do you have an image of the IR If so it would be.pdf                     Do you have an image of the IR If so it would be.pdf
Do you have an image of the IR If so it would be.pdfbrijmote
 
Cooperativity Two different theories of the coo.pdf
                     Cooperativity  Two different theories of the coo.pdf                     Cooperativity  Two different theories of the coo.pdf
Cooperativity Two different theories of the coo.pdfbrijmote
 
i) Lithium oxide ii) Sodium hypochlorite iii) Strontium (II).pdf
i) Lithium oxide ii) Sodium hypochlorite iii) Strontium (II).pdfi) Lithium oxide ii) Sodium hypochlorite iii) Strontium (II).pdf
i) Lithium oxide ii) Sodium hypochlorite iii) Strontium (II).pdfbrijmote
 
Hominin is the subtribe of tribe Hominini. These belong to modern hu.pdf
Hominin is the subtribe of tribe Hominini. These belong to modern hu.pdfHominin is the subtribe of tribe Hominini. These belong to modern hu.pdf
Hominin is the subtribe of tribe Hominini. These belong to modern hu.pdfbrijmote
 

More from brijmote (20)

You can use Network Monitor or type netstat -a in your command promp.pdf
You can use Network Monitor or type netstat -a in your command promp.pdfYou can use Network Monitor or type netstat -a in your command promp.pdf
You can use Network Monitor or type netstat -a in your command promp.pdf
 
There are no statements given.SolutionThere are no statements .pdf
There are no statements given.SolutionThere are no statements .pdfThere are no statements given.SolutionThere are no statements .pdf
There are no statements given.SolutionThere are no statements .pdf
 
The answer is B- Lagging strand.Lagging strand of DNA is synthesi.pdf
The answer is B- Lagging strand.Lagging strand of DNA is synthesi.pdfThe answer is B- Lagging strand.Lagging strand of DNA is synthesi.pdf
The answer is B- Lagging strand.Lagging strand of DNA is synthesi.pdf
 
solution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdfsolution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdf
 
Organs of the digestive tract1.Mouth-2.Tongue-3.Epiglottis-.pdf
Organs of the digestive tract1.Mouth-2.Tongue-3.Epiglottis-.pdfOrgans of the digestive tract1.Mouth-2.Tongue-3.Epiglottis-.pdf
Organs of the digestive tract1.Mouth-2.Tongue-3.Epiglottis-.pdf
 
a. BeCl2. The Be has 2 bonds and no lone pairs, so it is linear. b..pdf
  a. BeCl2. The Be has 2 bonds and no lone pairs, so it is linear.  b..pdf  a. BeCl2. The Be has 2 bonds and no lone pairs, so it is linear.  b..pdf
a. BeCl2. The Be has 2 bonds and no lone pairs, so it is linear. b..pdf
 
u can feed air by possible putting zinc chloride.pdf
                     u can feed air by possible putting  zinc chloride.pdf                     u can feed air by possible putting  zinc chloride.pdf
u can feed air by possible putting zinc chloride.pdf
 
The ammonia molecule (NH3) has three pairs of ele.pdf
                     The ammonia molecule (NH3) has three pairs of ele.pdf                     The ammonia molecule (NH3) has three pairs of ele.pdf
The ammonia molecule (NH3) has three pairs of ele.pdf
 
single S.pdf
                     single                                      S.pdf                     single                                      S.pdf
single S.pdf
 
E)Mg2- note more electron filled in the valence.pdf
                     E)Mg2-  note more electron filled in the valence.pdf                     E)Mg2-  note more electron filled in the valence.pdf
E)Mg2- note more electron filled in the valence.pdf
 
CH4 (methane) molecules only have london dispersi.pdf
                     CH4 (methane) molecules only have london dispersi.pdf                     CH4 (methane) molecules only have london dispersi.pdf
CH4 (methane) molecules only have london dispersi.pdf
 
PbCl4 - lead tetra chloride PbSo4 - lead sulfate.pdf
                     PbCl4 - lead tetra chloride  PbSo4 - lead sulfate.pdf                     PbCl4 - lead tetra chloride  PbSo4 - lead sulfate.pdf
PbCl4 - lead tetra chloride PbSo4 - lead sulfate.pdf
 
NiCl2 for sure. None of the rest are soluble in w.pdf
                     NiCl2 for sure. None of the rest are soluble in w.pdf                     NiCl2 for sure. None of the rest are soluble in w.pdf
NiCl2 for sure. None of the rest are soluble in w.pdf
 
It is polar. That is part of the reason it mixes .pdf
                     It is polar. That is part of the reason it mixes .pdf                     It is polar. That is part of the reason it mixes .pdf
It is polar. That is part of the reason it mixes .pdf
 
D. R-OH .pdf
                     D. R-OH                                      .pdf                     D. R-OH                                      .pdf
D. R-OH .pdf
 
D intermolecular forces are important when the so.pdf
                     D intermolecular forces are important when the so.pdf                     D intermolecular forces are important when the so.pdf
D intermolecular forces are important when the so.pdf
 
Do you have an image of the IR If so it would be.pdf
                     Do you have an image of the IR If so it would be.pdf                     Do you have an image of the IR If so it would be.pdf
Do you have an image of the IR If so it would be.pdf
 
Cooperativity Two different theories of the coo.pdf
                     Cooperativity  Two different theories of the coo.pdf                     Cooperativity  Two different theories of the coo.pdf
Cooperativity Two different theories of the coo.pdf
 
i) Lithium oxide ii) Sodium hypochlorite iii) Strontium (II).pdf
i) Lithium oxide ii) Sodium hypochlorite iii) Strontium (II).pdfi) Lithium oxide ii) Sodium hypochlorite iii) Strontium (II).pdf
i) Lithium oxide ii) Sodium hypochlorite iii) Strontium (II).pdf
 
Hominin is the subtribe of tribe Hominini. These belong to modern hu.pdf
Hominin is the subtribe of tribe Hominini. These belong to modern hu.pdfHominin is the subtribe of tribe Hominini. These belong to modern hu.pdf
Hominin is the subtribe of tribe Hominini. These belong to modern hu.pdf
 

Recently uploaded

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 

Recently uploaded (20)

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 

#include iostream using namespace std; void InsertionSort(int .pdf

  • 1. #include using namespace std; void InsertionSort(int arr[],int size){ int temp,j; for(int i=0; i=0){ arr[j+1] = arr[j]; j--; } arr[j+1] = temp; } } // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; /* create temp arrays */ int L[n1], R[n2]; /* Copy data to temp arrays L[] and R[] */ for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1+ j]; /* Merge arrays back into arr[l..r]*/ i = 0; // first subarray index j = 0; // second subarray index k = l; // merged subarray index while (i < n1 && j < n2) {
  • 2. if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy the remaining elements of L[], if there are any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy the remaining elements of R[], if there are any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } /* l is for left index and r is right index of the sub-array of arr to be sorted */ void mergeSort(int arr[], int l, int r) {
  • 3. if (l < r) { // Same as (l+r)/2, but avoids overflow for // large l and h int m = l+(r-l)/2; // Sort first and second halves mergeSort(arr, l, m); mergeSort(arr, m+1, r); merge(arr, l, m, r); } } void printArray(int arr[], int size){//Function to print array int i; for (i=0; i < size; i++) cout << arr[i] <<" "; cout << endl; } int main(){ int size = 20; int array1[] = {17,33,88,54,96,82,12,36,69,74,79,66,32,11,1,98,76,55,3,4}; int array2[] = {20,1,19,2,18,3,17,4,16,5,15,6,14,7,13,8,12,9,11,10}; int array3[] = {220,209,198,187,176,165,154,143,132,121,110,99,88,77,66,55,44,33,22,11}; int array4[] = {25,27,23,2,7,99,96,1996,85,3,24,11,17,4,1,0,8,9,98,45}; int array5[] = {15,35,85,100,40,90,70,65,25,5,10,30,55,75,80,95,20,45,50,60}; //Output for array1 cout << "Original array 1: "; printArray(array1, size); InsertionSort(array1, size); cout << "Array 1 sorted by insertion sort: "; printArray(array1, size); //CALL THE MERGE SORT HERE
  • 4. //PRINT THE MERGE SORT HERE cout << " "; //Output for array2 cout << "Original array 2: "; printArray(array2, size); mergeSort(array2,0,size); cout << "Array 2 sorted by Merge sort: "; printArray(array2, size); cout << " "; //Output for array3 cout << "Original array 3: "; printArray(array3, size); InsertionSort(array3, size); cout << "Array 3 sorted by insertion sort: "; printArray(array3, size); cout << " "; //Output for array4 cout << "Original array 4: "; printArray(array4, size); mergeSort(array4,0,size); cout << "Array 4 sorted by Merge sort: "; printArray(array4, size); cout << " "; //Output for array5 cout << "Original array 5: "; printArray(array5, size); InsertionSort(array5, size); cout << "Array 5 sorted by insertion sort: "; printArray(array5, size); cout << " "; return 0;
  • 5. } /* sample output Note: As sorting an already sorted array does not result good. I have changed the array2,array4 sorting to merge sorts */ Solution #include using namespace std; void InsertionSort(int arr[],int size){ int temp,j; for(int i=0; i=0){ arr[j+1] = arr[j]; j--; } arr[j+1] = temp; } } // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; /* create temp arrays */ int L[n1], R[n2]; /* Copy data to temp arrays L[] and R[] */ for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1+ j];
  • 6. /* Merge arrays back into arr[l..r]*/ i = 0; // first subarray index j = 0; // second subarray index k = l; // merged subarray index while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy the remaining elements of L[], if there are any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy the remaining elements of R[], if there are any */ while (j < n2) { arr[k] = R[j]; j++; k++;
  • 7. } } /* l is for left index and r is right index of the sub-array of arr to be sorted */ void mergeSort(int arr[], int l, int r) { if (l < r) { // Same as (l+r)/2, but avoids overflow for // large l and h int m = l+(r-l)/2; // Sort first and second halves mergeSort(arr, l, m); mergeSort(arr, m+1, r); merge(arr, l, m, r); } } void printArray(int arr[], int size){//Function to print array int i; for (i=0; i < size; i++) cout << arr[i] <<" "; cout << endl; } int main(){ int size = 20; int array1[] = {17,33,88,54,96,82,12,36,69,74,79,66,32,11,1,98,76,55,3,4}; int array2[] = {20,1,19,2,18,3,17,4,16,5,15,6,14,7,13,8,12,9,11,10}; int array3[] = {220,209,198,187,176,165,154,143,132,121,110,99,88,77,66,55,44,33,22,11}; int array4[] = {25,27,23,2,7,99,96,1996,85,3,24,11,17,4,1,0,8,9,98,45}; int array5[] = {15,35,85,100,40,90,70,65,25,5,10,30,55,75,80,95,20,45,50,60}; //Output for array1 cout << "Original array 1: ";
  • 8. printArray(array1, size); InsertionSort(array1, size); cout << "Array 1 sorted by insertion sort: "; printArray(array1, size); //CALL THE MERGE SORT HERE //PRINT THE MERGE SORT HERE cout << " "; //Output for array2 cout << "Original array 2: "; printArray(array2, size); mergeSort(array2,0,size); cout << "Array 2 sorted by Merge sort: "; printArray(array2, size); cout << " "; //Output for array3 cout << "Original array 3: "; printArray(array3, size); InsertionSort(array3, size); cout << "Array 3 sorted by insertion sort: "; printArray(array3, size); cout << " "; //Output for array4 cout << "Original array 4: "; printArray(array4, size); mergeSort(array4,0,size); cout << "Array 4 sorted by Merge sort: "; printArray(array4, size); cout << " "; //Output for array5 cout << "Original array 5: ";
  • 9. printArray(array5, size); InsertionSort(array5, size); cout << "Array 5 sorted by insertion sort: "; printArray(array5, size); cout << " "; return 0; } /* sample output Note: As sorting an already sorted array does not result good. I have changed the array2,array4 sorting to merge sorts */