JAVAmerge two array by order

Posted yutingliuyl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVAmerge 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("\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);
	}
}


以上是关于JAVAmerge two array by order的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode --- 1460. Make Two Arrays Equal by Reversing Subarrays 解题报告

LintcodeMedian of two Sorted Arrays

Divide by three, multiply by two CodeForces - 977D (思维排序)

Count and Say (Array Length Encoding) -- LeetCode

Python, pandas: how to sort dataframe by index// Merge two dataframes by index

Divide by three, multiply by two