#include <stdio.h>
#include <stdlib.h>#define NA 5
#define NB 5void Sort(int a[], int nLen)
{
for(int i = 0; i < nLen - 1; i++)
{
int n = i;
int p = a[i];
for(int j = i + 1; j < nLen; j++)
{
if(a[j] < p)
{
p = a[j];
n = j;
}
}
int t = a[i];
a[i] = a[n];
a[n] = t;
}
}int* Merge(int a[], int nLena, int b[], int nLenb)
{
int* c = (int*)malloc((nLena + nLenb)*sizeof(int));
int i = 0;
int j = 0;
int n =0;
while (i < nLena && j < nLenb)
{
if (a[i] <= b[j])
{
c[n++] = a[i++];
}
else
{
c[n++] = b[j++];
}
}
if (i < nLena)
{
for (; i < nLena; i++)
{
c[n++] = a[i];
}
}
if (j < nLenb)
{
for (; j < nLenb; j++)
{
c[n++] = b[j];
}
}
return c;
}void main()
{
int a[NA] = {0};
int b[NB] = {0};
printf("Input %d number for a[]: ", NA);
for (int i = 0; i < NA; i++)
{
scanf("%d", &a[i]);
}
printf("Input %d number for b[]: ", NB);
for (i = 0; i < NB; i++)
{
scanf("%d", &b[i]);
}
Sort(a, NA);
Sort(b, NB);
printf("a[] after sort: ");
for (i = 0; i < NA; i++)
{
printf("%d ", a[i]);
}
printf("\nb[] after sort: ");
for (i = 0; i < NB; i++)
{
printf("%d ", b[i]);
}
int *c = Merge(a, NA, b, NB);
printf("\na[] b[] after merge: ");
for (i = 0; i < NA + NB; i++)
{
printf("%d ", c[i]);
}
printf("\n");
}
温馨提示:内容为网友见解,仅供参考