SlideShare a Scribd company logo
#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

PDF
c++ program I need to sort arrays using an insertion sort and a mer.pdf
PDF
Merge sort
PPTX
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
PDF
Write a program that obtains the execution time of selection sort, bu.pdf
PDF
Chapter13 two-dimensional-array
PPTX
unit-2-dsa.pptx
PPT
SP-First-Lecture.ppt
DOCX
DAA Lab Work.docx
c++ program I need to sort arrays using an insertion sort and a mer.pdf
Merge sort
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
Write a program that obtains the execution time of selection sort, bu.pdf
Chapter13 two-dimensional-array
unit-2-dsa.pptx
SP-First-Lecture.ppt
DAA Lab Work.docx

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

PPT
array2d.ppt
PPT
2DArrays.ppt
PDF
codes.txt.pdf code presentation engineering
DOCX
PRACTICAL COMPUTING
PDF
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
PDF
programs on arrays.pdf
PPTX
2- Dimensional Arrays
PDF
SlideSet_4_Arraysnew.pdf
PDF
An Introduction to Part of C++ STL
PDF
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
PDF
I have written the code but cannot complete the assignment please help.pdf
PPTX
arrays.pptx
DOCX
Array
PPTX
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
PPTX
Java arrays
PPT
array2d.ppt
2DArrays.ppt
codes.txt.pdf code presentation engineering
PRACTICAL COMPUTING
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
programs on arrays.pdf
2- Dimensional Arrays
SlideSet_4_Arraysnew.pdf
An Introduction to Part of C++ STL
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
I have written the code but cannot complete the assignment please help.pdf
arrays.pptx
Array
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
Java arrays
Ad

More from brijmote (20)

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

Recently uploaded (20)

PDF
01-Introduction-to-Information-Management.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
RMMM.pdf make it easy to upload and study
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Classroom Observation Tools for Teachers
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Cell Structure & Organelles in detailed.
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Institutional Correction lecture only . . .
01-Introduction-to-Information-Management.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
RMMM.pdf make it easy to upload and study
Final Presentation General Medicine 03-08-2024.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
A systematic review of self-coping strategies used by university students to ...
102 student loan defaulters named and shamed – Is someone you know on the list?
Classroom Observation Tools for Teachers
O7-L3 Supply Chain Operations - ICLT Program
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Cell Structure & Organelles in detailed.
STATICS OF THE RIGID BODIES Hibbelers.pdf
Microbial diseases, their pathogenesis and prophylaxis
202450812 BayCHI UCSC-SV 20250812 v17.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Pharma ospi slides which help in ospi learning
Computing-Curriculum for Schools in Ghana
Institutional Correction lecture only . . .

#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 */