有两个按数据元素值递增有序排列的顺序表A和B,将A表和B表归并成一个按元素值递增有序排列的顺序表C
http://www.cnblogs.com/919czzl/p/4436917.html
public class MergeArray {
private static int[] a = { 1, 3, 5, 7, 9, 11, 13 };
private static int[] b = { 2, 4, 6, 8, 10, 12, 14, 15, 16 };
public static int[] mergeArray(int[] a, int[] b) {
int[] c = new int[a.length + b.length];
int i = 0, j = 0, k = 0;
while (i < a.length && j < b.length) {
if (a[i] < b[j]) {
c[k++] = a[i++];
} else {
c[k++] = b[j++];
}
}
while (i < a.length) {
c[k++] = a[i++];
}
while (j < b.length) {
c[k++] = b[j++];
}
return c;
}
public static void main(String[] args) {
int[] c = mergeArray(a, b);
System.out.println(a.length + "-" + b.length + "-" + c.length);
for (int i : c) {
System.out.print(i + " ");
}
}
}