Java Merge Sort mergeSort(int[] array)

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

Description

merge Sort

License

Open Source License

Declaration

public static void mergeSort(int[] array) 

Method Source Code

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

public class Main {
    public static void mergeSort(int[] array) {
        mergeSort(array, 0, array.length - 1);
    }/*from   www . j  ava  2s.c  o  m*/

    public static void mergeSort(int[] array, int i, int max) {
        if (i == max)
            return;
        int mid = (max + i) / 2;
        mergeSort(array, i, mid);
        mergeSort(array, mid + 1, max);

        merge(array, i, mid, max);
    }

    public static void merge(int[] array, int i, int mid, int max) {
        int[] tmp = new int[max - i + 1];
        int k = 0;
        int left = i;
        int right = mid + 1;
        while ((left <= mid) && (right <= max)) {
            if (array[left] <= array[right])
                tmp[k++] = array[left++];
            else
                tmp[k++] = array[right++];
        }
        while (left <= mid) {
            tmp[k++] = array[left++];
        }
        while (right <= max) {
            tmp[k++] = array[right++];
        }
        for (int j = 0; j < tmp.length; j++)
            array[i + j] = tmp[j];
    }
}

Related

  1. mergesort(double[] a, int[] b, int p, int r)
  2. mergeSort(int[] a)
  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. mergeSorted(float[] a, int alen, float b[], int blen)