合并两个有序数组
2018-09-12 本文已影响0人
ONEay
数组1:[5, 6, 7, 8]
数组2:[1, 7, 9]
结果数组:[1, 5, 6, 7, 7, 8, 9]
/**
* 合并两个有序数组
* @param args0
* @param args1
*/
public static void sort(int[] args0,int[] args1) {
//args0数组下标
int a = 0;
//args1数组下标
int b = 0 ;
int len = args0.length + args1.length;
//结果数组
int[] result = new int[len];
for (int i = 0; i < len; i++) {
//默认值为最大值
int numA =Integer.MAX_VALUE;
int numB =Integer.MAX_VALUE;
if (a < args0.length){
numA = args0[a];
}
if (b < args1.length){
numB = args1[b];
}
if (numA >= numB){
result[i] = numB;
b++;
}else {
result[i] = numA;
a++;
}
}
System.out.println(Arrays.toString(result));
}