Here you can find the source of sortedMerge(int[] a, int[] b)
public static int[] sortedMerge(int[] a, int[] b)
//package com.java2s; //License from project: Apache License public class Main { public static int[] sortedMerge(int[] a, int[] b) { int[] res = new int[a.length + b.length]; int i = 0, j = 0; for (int k = 0; k < res.length; ++k) { if (i == a.length) { System.arraycopy(b, j, res, k, res.length - k); j = b.length;/* w w w. j a va 2 s .co m*/ break; } if (j == b.length) { System.arraycopy(a, i, res, k, res.length - k); i = a.length; break; } res[k] = (a[i] > b[j]) ? b[j++] : a[i++]; } assert i == a.length && j == b.length; return res; } public static void sortedMerge(int[] aIds, double[] aVals, int[] bIds, double[] bVals, int[] resIds, double[] resVals) { int i = 0, j = 0; for (int k = 0; k < resIds.length; ++k) { if (i == aIds.length) { System.arraycopy(bIds, j, resIds, k, resIds.length - k); System.arraycopy(bVals, j, resVals, k, resVals.length - k); j = bIds.length; break; } if (j == bIds.length) { System.arraycopy(aIds, i, resIds, k, resIds.length - k); System.arraycopy(aVals, i, resVals, k, resVals.length - k); i = aIds.length; break; } if (aIds[i] > bIds[j]) { resIds[k] = bIds[j]; resVals[k] = bVals[j]; ++j; } else { resIds[k] = aIds[i]; resVals[k] = aVals[i]; ++i; } } assert i == aIds.length && j == bIds.length; } }