以下是引用执笔画江山在2015-4-5 15:16:43的发言:
void Merge(int c[], int *nc,int a[],int na,int b[], int nb)
{
int cursora = 0;
int cursorb = 0;
int cursorc = 0;
while((cursora<na)&&(cursorb<nb))
{
if (a[cursora]< b[cursorb])//这里加上等号 注掉下面的else if 就是一个归并 不去重
{
c[cursorc++]=a[cursora++];
}
else if(a[cursora] = b[cursorb])
{
c[cursorc++]=a[cursora++];
cursorb++; //现在是去重合并。加上elseif 上面的<= 改为< 如果这里只有 cursorb++; cursora++; 那么就是将重复的直接删掉
}
else
{
c[cursorc++]=b[cursorb++];
}
}
while(cursora<na)
{
c[cursorc++]=a[cursora++];
}
while(cursorb<nb)
{
c[cursorc++]=b[cursorb++];
}
*nc = cursorc;
}
改动的地方在我汉字注释的地方
就这样的代码也能解决问题??
我很是怀疑
还是帮你一把吧
程序代码:
#include <stdio.h>
#include <stdlib.h>
int merger_sort(int *a, int *x, int lenx, int *y, int leny) {
int i, *pa = a, *px = x, *py = y;
*pa = (*px < *py) ? *px++ : *py++;
while(px < x + lenx && py < y + leny) {
if(*pa != ((*px < *py) ? *px : *py))
*++pa = (*px < *py) ? *px++ : *py++;
else {
if(*pa == *px) px++;
if(*pa == *py) py++;
}
}
pa++;
if(px == x + lenx) {
while(py < y + leny) {
for(i = 0; i < pa - a; i++)
if(a[i] == *py) break;
if(i == pa - a) *pa++ = *py;
py++;
}
} else {
while(px < x + lenx) {
for(i = 0; i < pa - a; i++)
if(a[i] == *px) break;
if(i == pa - a) *pa++ = *px;
px++;
}
}
return pa - a;
}
int main(void) {
int i;
int x[] = {-1, 0, 1, 2, 3, 3, 4, 5, 5, 6, 7, 8, 9, 9};
int y[] = {-2, 0, 1, 2, 2, 3, 4, 5, 6, 6, 7};
int lenx = sizeof(x) / sizeof(x[0]);
int leny = sizeof(y) / sizeof(y[0]);
int *a = malloc((lenx + leny) * sizeof(int));
if(!a) exit(EXIT_FAILURE);
for(i = 0; i < merger_sort(a, x, lenx, y, leny); i++)
printf("%d ", a[i]);
puts("");
free(a);
return 0;
}