void merge(int *input1, size_t sz1,
int *input2, size_t sz2,
int *output, size_t sz3) {
int i = 0;
int index1 = 0, index2 = 0;
while (i < sz3 && index1 < sz1 && index2 < sz2)
if (input1[index1] <= input2[index2])
output[i++] = input1[index1++];
else
output[i++] = input2[index2++];
if (index1 < sz1)
for (; i < sz3 && index1 < sz1; ++i, ++index1)
output[i] = input1[index1];
else if (index2 < sz2)
for (; i < sz3 && index2 < sz2; ++i, ++index2)
output[i] = input2[index2];
}
который вы используете таким образом:
#define TAB_SIZE(x) (sizeof(x)/sizeof(*(x)))
int tab1[] = { 1, 2, 4, 6 };
int tab2[] = { 3, 5, 7 };
int tabMerged[TAB_SIZE(tab1)+TAB_SIZE(tab2)];
merge(tab1, TAB_SIZE(tab1), tab2, TAB_SIZE(tab2), tabMerged, TAB_SIZE(tabMerged));