#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,a.
#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
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: ";
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
*/