import java.util.Arrays;
public class MergeSort {
public static void main(String[] args) {
int []arr= {9,8,7,6};
sort(arr);
System.out.println(Arrays.toString(arr));
}
public static void sort(int[] arr){
int []temp = new int[arr.length];
sort(arr,0,arr.length - 1,temp);
}
public static void sort(int[] arr, int left, int right,int[] temp){
if(left == right)
return;
int mid = (right + left) / 2;
sort(arr, left, mid, temp);
sort(arr, mid + 1, right, temp);
merge(arr, left, mid, right, temp);
}
public static void merge(int[] arr, int left, int mid , int right, int[] temp){
int i = left;
int j = mid + 1;
int t = 0;
while(i <= mid && j <= right){
if(arr[i] <= arr[j]){
temp[t++] = arr[i++];
}else{
temp[t++] = arr[j++];
}
}
while (i <= mid){
temp[t++] = arr[i++];
}
while(j <= right){
temp[t++] = arr[j++];
}
t = 0;
while(left <= right){
arr[left++] = temp[t++];
}
}
}
时间复杂度
自己需要注意的几点
在传入arr.length的时候记得减1
以及在下面mergeSort中第三个<= ,防止相等的时候,要让左面的先入