Java Merge Sort mergeSorted(float[] a, int alen, float b[], int blen)

Here you can find the source of mergeSorted(float[] a, int alen, float b[], int blen)

Description

merge Sorted

License

Open Source License

Declaration

public static float[] mergeSorted(float[] a, int alen, float b[], int blen) 

Method Source Code

//package com.java2s;

public class Main {
    public static float[] mergeSorted(float[] a, int alen, float b[], int blen) {
        float[] answer = new float[alen + blen];
        int i = 0, j = 0, k = 0;
        while (i < alen && j < blen) {
            if (a[i] < b[j])
                answer[k++] = a[i++];/* w w w  .j a v  a  2  s .c o m*/
            else
                answer[k++] = b[j++];
        }
        while (i < alen)
            answer[k++] = a[i++];
        while (j < blen)
            answer[k++] = b[j++];
        return answer;
    }

    public static float[] mergeSorted(float[] a, float b[]) {
        float[] answer = new float[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++];
            else
                answer[k++] = b[j++];
        }
        while (i < a.length)
            answer[k++] = a[i++];
        while (j < b.length)
            answer[k++] = b[j++];
        return answer;
    }

    public static double[] mergeSorted(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++];
            else
                answer[k++] = b[j++];
        }
        while (i < a.length)
            answer[k++] = a[i++];
        while (j < b.length)
            answer[k++] = b[j++];
        return answer;
    }

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

Related

  1. mergeSort(int[] a)
  2. mergeSort(int[] array)
  3. mergeSort(long[] theArray, int nElems)
  4. mergeSort(Object[] src, Object[] dest, int low, int high, int off)
  5. mergeSort(T[] src, T[] dst, int start, int end)
  6. mergeSortedAaary(int[] a, int[] b)
  7. mergeSortedArrays(double[] a, double[] b)