数组b:int[] b = { 2, 4, 12, 34, 55, 66, 99 };// 1 2 12 34 55 66 99
打印数组c:“1 2 4 5 6 9 12 12 34 55 66 99”
程序代码如下:
int[] a = { 1, 12, 5, 6, 9 };
int[] b = { 2, 4, 12, 34, 55, 66, 99 };// 1 2 12 34 55 66 99
int alength = a.length;
int blength = b.length;
int[] c = new int[alength + blength];
if (alength > blength) {
for (int j = 0; j < alength; j++) {
c[j] = a[j];
}
//同理如else中
} else {
//遍历赋值
for (int j = 0; j < blength; j++) {
c[j] = b[j];
}
for (int i = 0; i < a.length; i++) {
int choose = a[i];// 取出的值
out: for (int k = 0; k < c.length; k++) {
if (choose <= c[k]) {
int s = c.length;
// 移动插入位置之后的数据(查找c中比c[i]值小的做插入)
for (; s >= k; s--) {
if ((s + 1) < c.length) {
c[s + 1] = c[s];
}
}
// 插入数据
c[k] = a[i];
break out;
}
}
}
}
//做打印输出
for (int i : c) {
System.out.print(i+" ");
}
数组的排序升序(比大小)和降序(比小大)