package com.dengpf.interview;
/**
* Created by kobe73er on 16/11/11.
*/
public class ClientInterview {
public static int[] getSortedArray(int a[], int b[]) {
int result[] = new int[a.length + b.length];
int i = 0;
int j = 0;
int k = 0;
while (i < a.length && j < b.length) {
if (a[i] < b[j]) {
result[k++] = a[i++];
} else {
result[k++] = b[j++];
}
}
while (i < a.length) {
result[k++] = a[i++];
}
while (j < b.length) {
result[k++] = b[j++];
}
return result;
}
public static void main(String args[]) {
int a[] = {1, 2, 3, 4, 5, 5, 5};
int b[] = {6, 7, 8, 9, 10, 34, 56};
int[] result = getSortedArray(a, b);
for (int index = 0; index < result.length; index++) {
System.out.println(result[index]);
}
}
}
两个有序数组合并为一个有序数组
最新推荐文章于 2022-05-17 20:39:20 发布
本文介绍了一个简单的数组合并排序算法实现。该算法将两个已排序的整数数组合并为一个有序数组。通过对两个输入数组进行遍历比较,将较小的元素依次放入结果数组中,直至所有元素被处理完毕。
1796

被折叠的 条评论
为什么被折叠?



