首页 > 编程语言 > 详细

【JAVA】merge two array by order

时间:2014-06-26 08:16:24      阅读:270      评论:0      收藏:0      [点我收藏+]
public class MergeSort {
	static void show(int a[]) {
		int i;
		for (i = 0; i < a.length; i++) {
			System.out.print(a[i]+"-");
		}
		System.out.println("\n");
	}
	static void merge(int arr1[], int arr2[], int res[]) {
		int i=0,j=0;
		int idx = 0;
		for (;;) {
			System.out.print("show res:");
			show(res);
			if(i>=10 || j>=10)break;
			if (arr1[i] <= arr2[j]) {
				res[idx] = arr1[i];
				i++;
			} else {
				res[idx] = arr2[j];
				j++;
			}
			idx++;
		}
		if(i<10){
			for(;i<10;i++){
				res[idx] = arr1[i];
				idx++;
			}
		}
		if(j<10){
				for(;j<10;j++){
					res[idx] = arr1[j];
					idx++;
				}
			}
		return;
	}
	public static void main(String args[]) {
		int arr1[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
		int arr2[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
		show(arr1);
		show(arr2);
		int res[] = new int[20];
		show(res);
		merge(arr1, arr2, res);
		System.out.print("final:");show(res);
	}
}

【JAVA】merge two array by order,布布扣,bubuko.com

【JAVA】merge two array by order

原文:http://blog.csdn.net/xiewenbo/article/details/34420443

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