Java Array Sort sortedMerge(int[] a, int[] b)

Here you can find the source of sortedMerge(int[] a, int[] b)

Description

sorted Merge

License

Apache License

Declaration

public static int[] sortedMerge(int[] a, int[] b) 

Method Source Code

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

Related

  1. sorted(int[] array)
  2. sortedArraysEqual(Object[] arr1, Object[] arr2)
  3. sortedCopyOf(final int[] array)
  4. sortedIndex(double[] values)
  5. sortedIndexOf(Comparable[] list, Comparable val, int lower, int higher)
  6. sortedMerge(int[] aIds, double[] aVals, int[] bIds, double[] bVals, int[] resIds, double[] resVals)
  7. sortedPointersInto(double d[])
  8. sortedPointersInto_tryingToImproveSpeed(final double d[])
  9. sortedPointersInto_usingStrictfp(final double d[])