【JAVA】merge two array by order

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("
");
	}
	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);
	}
}

原文地址:https://www.cnblogs.com/yutingliuyl/p/6851693.html