1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
#include <stdio.h>
#include <iostream>
using std::cout;
using std::endl;
void merge(int a1[],int n,int a2[],int m)
{
int i=0,j=0,k=0,t,total=n+m;
int a[20];
for(int x=0; x<n;x++)
a[x]=a1[x];
for(int x=0; x<m;x++)
a[x+n] = a2[x]; // Combine both arrays into the one array
for(t=0;t<total;t++)
{
printf("%d\t",a[t]); // Print out un-sorted aray
}
for(int i = 1; i < total; i++) // Sort the larger array
{
for(int j = 0; j < total - 1; j++)
{
if(a[j] > a[j+1])
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for(t=0;t<total;t++)
{
printf("%d\t",a[t]); // Print out sorted array
}
}
int main()
{
int a1[]={25,29,34,45};
int a2[]={28,31,33,39,40,42};
merge(a1,4,a2,6);
}
|