首页 > 代码库 > CC150 - 11.1
CC150 - 11.1
Question:
You are given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B.
Write a method to merge B into A in sorted order.
package POJ;public class Main { /** * * 11.1 You are given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B. * Write a method to merge B into A in sorted order. * */ public static void main(String[] args) { Main so = new Main(); } public void merge(int[] a, int[] b, int lastA, int lastB){ int indexA=lastA-1; int indexB=lastB-1; int indexMerged=lastA+lastB-1; while(indexA>=0&&indexB>=0){ if(a[indexA]<b[indexB]){ a[indexMerged]=b[indexB]; indexB--; indexMerged--; }else{ a[indexMerged]=a[indexA]; indexA--; indexMerged--; } } while(indexB>=0){ a[indexMerged]=b[indexB]; indexB--; indexMerged--; } }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。