首页 > 其他 > 详细

CC150 - 11.1

时间:2014-08-19 23:51:25      阅读:470      评论:0      收藏:0      [点我收藏+]

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--;
        }
    }
}

 

CC150 - 11.1,布布扣,bubuko.com

CC150 - 11.1

原文:http://www.cnblogs.com/Phoebe815/p/3923403.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!