用图了解归并排序
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8d1c65d7b1f3fc2d2855c7db923ebca4.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a781aa6473fcb8fd1cd637e1bcfd4c84.png)
class Msort
import java.util.Arrays;
public class Msort {
public static void main(String[] args) {
int[] arrays= new int[]{1,4,2,3,7,6,5,9,8,0};
int[] temp=new int[arrays.length];
sort(arrays,0, arrays.length-1,temp);
System.out.println(Arrays.toString(arrays));
}
public static void sort(int [] arrays,int left,int right,int [] temp){
if(left<right){
int mid=(left+right)/2;
sort(arrays,left,mid,temp);
sort(arrays,mid+1,right,temp);
sum(arrays,left,right,mid,temp);
}
}
public static void sum(int[] arrays,int left,int right,int mid,int[] temp){
int i=left;
int j=mid+1;
int t=0;
while (i<=mid&&j<=right){
if(arrays[i]>arrays[j]){
temp[t]=arrays[j];
t++;
j++;
}else{
temp[t]=arrays[i];
t++;
i++;
}
}
while (i<=mid){
temp[t]=arrays[i];
t++;
i++;
}
while(j<=right){
temp[t]=arrays[j];
t++;
j++;
}
int tempIndex=left;
int k=0;
while(tempIndex<=right){
arrays[tempIndex]=temp[k];
tempIndex++;
k++;
}
}
}