#include <stdio.h>
int merge_set(int a[],int la, int b[], int lb,int c[])
{
int i=0;
int j=0;
int k=0;
int tmp;
while( i<la && j<lb)
{
if(a[i]<b[j]) tmp=a[i++];
else tmp=b[j++];
if (k==0 || tmp!=c[k-1] )
c[k++]=tmp;
}
while (i<la)
{
if (k==0 || a[i]!=c[k-1] )
{
c[k]=a[i];
k++;
}
i++;
}
while (j<lb)
{
if ( k==0 || b[j]!=c[k-1] )
{
c[k]=b[j];
k++;
}
j++;
}
return k;
}
void print(int a[], int la)
{
int i;
for(i=0;i<la;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
int main()
{
int a[6]={3,5,8,8,11,11};
int b[12]={2,3,6,8,11,11,11,15,15,20,23,26};
int la=6;
int lb=12;
int c[la+lb];
int lc;
lc=merge_set(a,la,b,lb,c);
print(c,lc);
printf("\nlength is %d\n",lc);
return 0;
}
|