package sort;

import java.util.Arrays;

/**
 * Created by whydk on 2016/9/5.
 */
public class MergingSort {
    int a[] = {49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51};

    public void mergingSort() {
        sort(a, 0, a.length - 1);

        for (int i = 0; i < a.length; i++)
            System.out.println(a[i]);
    }

    public void sort(int[] data, int left, int right) {

        if (left < right) {
            //找出中间索引
            int center = (left + right) / 2;
            //对左边数组进行递归

            sort(data, left, center);
            //对右边数组进行递归
            sort(data, center + 1, right);

            //合并
            merge(data, left, center, right);
        }
    }

    public void merge(int[] data, int left, int center, int right) {

        int[] tmpArr = new int[data.length];
        int mid = center + 1;
        //third 记录中间数组的索引
        int third = left;
        int tmp = left;

        while (left <= center && mid <= right) {

            //从两个数组中取出最小的放入中间数组
            if (data[left] <= data[mid]) {
                tmpArr[third++] = data[left++];
            } else {
                tmpArr[third++] = data[mid++];
            }
        }

        //剩余部分依次放入中间数组
        while (mid <= right) {
            tmpArr[third++] = data[mid++];
        }

        while (left <= center) {
            tmpArr[third++] = data[left++];
        }

        //将中间数组中的内容复制回原数组
        while (tmp <= right) {
            data[tmp] = tmpArr[tmp++];
        }
        System.out.println(Arrays.toString(data));
    }

    public static void main(String[] args) {
        new MergingSort().mergingSort();
    }
}

@@ -105,134 +105,8 @@
 rt %7B
-%0A    int a%5B%5D = %7B49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51%7D;
 %0A%0A  
@@ -131,16 +131,23 @@
 ingSort(
+int%5B%5D a
 ) %7B%0A    
@@ -179,97 +179,115 @@
  1);
+%0A    %7D
 %0A%0A
+%0A
     
-    for (int i = 0; i %3C a.length; i++)%0A            System.out.println(a%5Bi%5D);
+/**%0A     *%0A     * @param data %E5%BE%85%E6%8E%92%E5%BA%8F%E6%95%B0%E7%BB%84%0A     * @param left %E6%95%B0%E6%8D%AE%E5%B7%A6%E4%B8%8B%E6%A0%87%0A     * @param right %E6%95%B0%E7%BB%84%E5%8F%B3%E4%B8%8B%E6%A0%87
 %0A    
-%7D%0A
+ */
 %0A   
@@ -637,32 +637,134 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     *%0A     * @param data%0A     * @param left%0A     * @param center%0A     * @param right%0A     */%0A
     public void 
@@ -1564,16 +1564,28 @@
 println(
+%22%5B data %5D %22+
 Arrays.t
@@ -1603,24 +1603,165 @@
 a));%0A    %7D%0A%0A
+    private static int a%5B%5D = %7B49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51%7D;%0A
     public s
@@ -1831,16 +1831,79 @@
 ingSort(
+a);%0A        System.out.println(%22%5B result %5D %22+Arrays.toString(a)
 );%0A    %7D

add doc