Java Array Merge mergeSortedInc(long[] a, long[] b)

Here you can find the source of mergeSortedInc(long[] a, long[] b)

Description

merge Sorted Inc

License

Apache License

Declaration

public static final long[] mergeSortedInc(long[] a, long[] b) 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.util.Arrays;

public class Main {
    public static final long[] mergeSortedInc(long[] a, long[] b) {
        assert isSortedInc(a) && isSortedInc(b);
        long[] res = new long[a.length + b.length];
        int ai = 0, bi = 0;
        while (ai < a.length || bi < b.length) {
            if (a[ai] < b[bi]) {
                res[ai + bi] = a[ai];//from  w w  w.  j a v  a  2s  .com
                ai++;
            } else if (b[bi] < a[ai]) {
                res[ai + bi] = b[bi];
                bi++;
            } else
                throw new IllegalArgumentException(Arrays.toString(a) + "|" + Arrays.toString(b));
        }
        return res;
    }

    public static final boolean isSortedInc(long[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (i > 0 && arr[i] <= arr[i - 1])
                return false;
        }
        return true;
    }

    public static final boolean isSortedInc(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (i > 0 && arr[i] <= arr[i - 1])
                return false;
        }
        return true;
    }
}

Related

  1. mergeParameterVariableNameDescription(String[] parameterType, String[] variableName)
  2. mergerBy(String[] target, String seperator)
  3. mergeSort(Object[] src, Object[] dest, int low, int high, int off)
  4. mergeSort(Object[] src, Object[] dest, int low, int high, int off, Comparator c)
  5. mergeSortArrays(String[][] arrayArray)
  6. mergeStringArray(String[] array)
  7. mergeStringArray(String[] array, String seperator)
  8. mergeStringArray(String[] ary1, String[] ary2)
  9. mergeStringArray(String[] inp1, String[] inp2)