Java Merge Sort mergeSort(int[] a)

Here you can find the source of mergeSort(int[] a)

Description

Sorts a given array of integers in ascending order using a recursive merge sort.

License

Open Source License

Parameter

Parameter Description
a The array to sort using this algorithm.

Declaration

public static void mergeSort(int[] a) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from www . ja v a 2s .  c om
     * Sorts a given array of integers in ascending order using a recursive 
     * merge sort.
     * 
     * @param a The array to sort using this algorithm.
     */
    public static void mergeSort(int[] a) {
        merge(a, new int[a.length], 0, a.length - 1);
    }

    /**
     * Helper method for {@link #mergeSort(int[])}. Sorts the elements
     * between the indeces {@code fromIndex} and {@code toIndex} in the given 
     * {@code a} array.
     * 
     * @param a array to subsort using this algorithm. 
     * @param temp A temporary array to aid in the recursive merge process.
     * @param fromIndex The index to begin sorting in {@code a}.
     * @param toIndex The index to stop sorting in {@code a}.
     * @see #mergeSort(int[])
     * @see #finishMerge(int[], int, int, int, int[])
     */
    private static void merge(int[] a, int[] temp, int fromIndex, int toIndex) {
        if (fromIndex < toIndex) {
            int middleIndex = (fromIndex + toIndex) / 2;
            merge(a, temp, fromIndex, middleIndex);
            merge(a, temp, middleIndex + 1, toIndex);
            finishMerge(a, temp, fromIndex, middleIndex, toIndex);
        }
    }

    /**
     * Merges two adjacent array parts, each of which has been already sorted in
     * ascending order.
     * 
     * @param a The array to sort using this algorithm.
     * @param temp A temporary array to aid in the recursive merge process.
     * @param fromIndex The index to begin sorting in {@code a}.
     * @param middleIndex The ending index of {@code a} of this part of the 
     *        merge process.
     * @param toIndex The index to stop sorting in {@code a}.
     * @see #mergeSort(int[]) 
     * @see #merge(int[], int, int, int[]) 
     */
    private static void finishMerge(int a[], int[] temp, int fromIndex, int middleIndex, int toIndex) {
        int i = fromIndex;
        int j = middleIndex + 1;
        int k = fromIndex;
        while (i <= middleIndex && j <= toIndex) {
            if (a[i] < a[j]) {
                temp[k] = a[i];
                i++;
            } else {
                temp[k] = a[j];
                j++;
            }
            k++;
        }
        while (i <= middleIndex) {
            temp[k] = a[i];
            i++;
            k++;
        }
        while (j <= toIndex) {
            temp[k] = a[j];
            j++;
            k++;
        }
        for (k = fromIndex; k <= toIndex; k++) { // Fill and finish a[]int.
            a[k] = temp[k];
        }
    }
}

Related

  1. mergesort(double[] a, int[] b, int p, int r)
  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)