源码如下:
#include <stdio.h>
#define M 5
#define N 3
//归并排序
int main()
{
int a[M] = {2,3,5,7,9};
int b[N] = {2,4,6};
int c[M+N];
int x = 0,y = 0,t = 0;
while(x<M && y<N)
{
if(a[x] < b[y])
{
// c[t] = c[x];
// t++;
// x++;
// 等价于下面:
c[t++] = a[x++];
}
else
{
c[t++] = b[y++];
}
}
// if(x == M) //优化
// {
while(y<N)
{
c[t++] = b[y++];
}
// }
// if(y == N)
// {
while(x<M)
{
c[t++] = a[x++];
}
// }
for(int i=0;i<M+N;i++)
{
printf("c[%d] = %d\n",i,c[i]);
}
return 0;
}