Java Merge Sort mergeSortedArrays(double[] a, double[] b)

Here you can find the source of mergeSortedArrays(double[] a, double[] b)

Description

merge Sorted Arrays

License

Open Source License

Declaration

public static double[] mergeSortedArrays(double[] a, double[] b) 

Method Source Code

//package com.java2s;
/*/*from w ww  . j  a va  2 s.c o m*/
 * This file is part of Pathrate, an end-to-end capacity estimation tool
 * Copyright (C) 2002-2013
 *  Constantinos Dovrolis    <dovrolis@cc.gatech.edu>
 *  Ravi S Prasad            <ravi@cc.gatech.edu>
 *  Antonio Macr?            <ing.antonio.macri@gmail.com>
 *  Francesco Racciatti      <francesco.racciatti@gmail.com>
 *  Silvia Volpe             <silvia.volpe88@gmail.com>           
    
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static double[] mergeSortedArrays(double[] a, double[] b) {
        double[] answer = new double[a.length + b.length];
        int i = 0, j = 0, k = 0;
        while (i < a.length && j < b.length) {
            if (a[i] < b[j]) {
                answer[k] = a[i];
                i++;
            } else {
                answer[k] = b[j];
                j++;
            }
            k++;
        }
        while (i < a.length) {
            answer[k] = a[i];
            i++;
            k++;
        }
        while (j < b.length) {
            answer[k] = b[j];
            j++;
            k++;
        }
        return answer;
    }
}

Related

  1. mergeSort(long[] theArray, int nElems)
  2. mergeSort(Object[] src, Object[] dest, int low, int high, int off)
  3. mergeSort(T[] src, T[] dst, int start, int end)
  4. mergeSorted(float[] a, int alen, float b[], int blen)
  5. mergeSortedAaary(int[] a, int[] b)