#include <stdio.h> #include <stdlib.h>
void merge(int c[], int a[], int n, int b[], int m) { int i, j, k; for (i = 0, j = 0, k = 0; k < n+m; k++) { if (i == n) { c[k] = b[j++]; continue; } if (j == m) { c[k] = a[i++]; continue; } if (a[i] < b[j]) { c[k] = a[i]; i++; } else { c[k] = b[j]; j++; } } }
int main() { int a[] = {1, 20, 24, 42, 200, 10412}; int b[] = {2, 32, 53, 120, 322, 922, 4321};
int *c; int len = sizeof(a)/sizeof(a[0]) + sizeof(b)/sizeof(b[0]); c = (int *)malloc(len); if (c == NULL) { printf("malloc error\n"); return -1; }
merge(c, a, sizeof(a)/sizeof(a[0]), b, sizeof(b)/sizeof(b[0]));
int i; for (i = 0; i < len; i++) printf("%d ", c[i]); printf("\n"); return 0; }
|