合并两个排好序的数组

来源:互联网 发布:webp软件 编辑:程序博客网 时间:2024/06/03 21:06
public static void main(String[] args) {Integer[] a = {11,23,65,77,98};Integer[] b = {22,42,64,72,79};Integer[] c = new Integer[a.length+b.length];merge(a, b, c);System.out.println("\r\n" + "合并排序数组");Arrays.asList(c).forEach(e->System.out.print(e + "   "));}/** * 合并俩个排好序的数组 *  * @param a * @param b * @param c */private static void merge(Integer[] a, Integer[] b, Integer[] c){// 判断for (int i = 0, j = 0, k = 0 ; i < c.length; i++) {if (j == a.length) {System.arraycopy(b, k, c, i, c.length-i);break;}if (k == b.length) {System.arraycopy(a, j, c, i, c.length-i);break;}if (a[j] <= b[k]) {c[i] = a[j++];}else {c[i] = b[k++];}}}

阅读全文
0 0
原创粉丝点击