F How to merge two sorted arrays in such a way that the resultant array is also merged? | CodeTheta

How to merge two sorted arrays in such a way that the resultant array is also merged?

April 17, 2014

/* http://native-code.blogspot.com */

#include<stdio.h>
#include<conio.h>
#define MAX 20
void input(int*, int);
void merge(int*, int*, int*, int, int, int);
void b_sort(int*, int);
void display(int*, int);
void main()
{
int a[MAX], b[MAX], c[MAX], max1, max2, max3;
printf("/* http://native-code.blogspot.com */ \n\n Enter range of the first array:\n");
scanf("%d", &max1);
printf("\nEnter elements of the first array:\n");
input(a, max1);
printf("\nEnter range of the second array:\n");
scanf("%d", &max2);
printf("\nEnter elements of the second array:\n");
input(b, max2);
printf("\nFirst array:\n");
display(a, max1);
printf("\nSecond array:\n");
display(b, max2);
max3=max1+max2;
merge(a, b, c, max1, max2, max3);
b_sort(c, max3);
printf("\nAfter sorting:\n");
display(c, max3);
getch();
}
void input(int arr[], int max)
{
int i;
for(i=0; i<max; i++)
scanf("%d", &arr[i]);
}
void display(int arr[], int max)
{
int i;
for(i=0; i<max; i++)
printf("%d ", arr[i]);
}
void merge(int a[], int b[], int c[], int max1, int max2, int max3)
{
int i, j;
j=0;
for(i=0; i<max1; i++)
{
c[i]=a[i];
}
i=max1;
while(i<max3)
{
c[i]=b[j];
j++;
i++;
}
}
void b_sort(int arr[], int max)
{
int i, j, temp;
for(i=0; i<max-1; i++)
{
for(j=i+1; j<max; j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}

/* http://native-code.blogspot.com */

Post a Comment