https://s3-us-west-2.amazonaws.com/secure.notion-static.com/df88faef-98f1-4236-97c7-dcef9cf8bf6f/849589-20171015230557043-37375010.gif

package top.ltyzqhh.Annotation;

import java.util.Arrays;

public class MergeSort {
    public static void main(String[] args) {
        int[] a={1324,234,532,1313,1,3,553};
        int[] temp= new int[a.length];
        System.out.println(Arrays.toString(a));
        mergeSort(a,0,a.length-1,temp);
        System.out.println(Arrays.toString(a));
    }
    public static void mergeSort(int[] array,int left,int right,int[] temp){
        if (left<right){
            int mid=(left+right)/2;
            mergeSort(array,0,mid,temp);//将左边部分继续分
            mergeSort(array,mid+1,right,temp);//将右边部分继续分
            merge(array,left,mid,right,temp);//合
        }
    }
    public static void merge(int[] array,int left,int mid, int right,int[] temp){
        int i=left;
        int j=mid+1;
        int t=0;//表示临时数组的下标索引
        while (i<=mid && j<=right){
            if (array[i]<=array[j]){
                temp[t]=array[i];
                i++;
                t++;
            }else {
                temp[t]=array[j];
                j++;
                t++;
            }
            }
            //如果左边的部分没有合并,则接着i继续合并
        while (i<=mid){
                temp[t]=array[i];
                t++;
                i++;
            }
            //如果右边的部分没有合并,则接着j继续合并
        while (j<=right){
                temp[t]=array[j];
                t++;
                j++;
            }
            //接着将temp里面的数组填充到指定位置
        t=0;
        int tempLeft=left;
        while (tempLeft<=right)
            {
                array[tempLeft]=temp[t];
                t++;
                tempLeft++;
            }

    }
}