归并排序动态图:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8a644a683d682829cd1fbe9567a8398c.gif#pic_center)
测试代码:
package demo3;
import java.util.Arrays;
public class MergeSort {
public static void main(String[] args) {
int[] arr=new int[]{1,3,5,2,4,6,8,9};
System.out.println(Arrays.toString(arr));
mergeSort(arr,0,arr.length-1);
System.out.println(Arrays.toString(arr));
}
public static void mergeSort(int[] arr,int low,int high){
int middle=(high+low)/2;
if(low<high) {
mergeSort(arr,low,middle);
mergeSort(arr,middle+1,high);
merge(arr,low,middle,high);
}
}
public static void merge(int[] arr,int low,int middle,int high) {
int[] temp=new int[high-low+1];
int i=low;
int j=middle+1;
int index=0;
while(i<=middle&&j<=high) {
if(arr[i]<=arr[j]) {
temp[index]=arr[i];
i++;
}else {
temp[index]=arr[j];
j++;
}
index++;
}
while(j<=high) {
temp[index]=arr[j];
j++;
index++;
}
while(i<=middle) {
temp[index]=arr[i];
i++;
index++;
}
for(int k=0;k<temp.length;k++) {
arr[k+low]=temp[k];
}
}
}