首页 > 代码库 > 实现2个排好序的子序列合并
实现2个排好序的子序列合并
数组a,b为已排序好的升序序列。
思路:1. 将a,b数组copy到一个新的数组c中(数组c的长度为a,b之和)
2. 在c中以数组a为基准,当b中的数值小于a的时候,a中以后数值向后移1位,然后把当前b的值赋值过来。
具体实现:
public static int[] sort(int[] a, int[] b) { // 将a,b数组copy到数组c中 int[] c = new int[a.length + b.length]; System.arraycopy(a, 0, c, 0, a.length); System.arraycopy(b, 0, c, a.length, b.length); int i = 0, j = a.length, k; while (i < j) { if (j < c.length && c[i] > c[j]) { int tem = c[j]; for (k = j; k > i; k--) { c[k] = c[k - 1]; } c[i] = tem; i++; j++; } else { i++; } } return c; }
实现2个排好序的子序列合并
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。