1、第1版
import java.util.Arrays;
public class MergeSortDemo {
public static void main(String[] args) {
int[] arr = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
mergeSort(arr, 0, arr.length - 1);
}
private static void mergeSort(int[] arr, int low, int high) {
// 这里一定是小于号
if (low < high) {
int mid = (low + high) / 2;
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);
merge(arr, low, mid, high);
}
}
private static void merge(int[] arr, int low, int mid, int high) {
int left = low;
int right = mid + 1;
// 这里的temp长度其实可以不用和arr一样
int[] temp = new int[arr.length];
// 将current设为左部分的第一个下标
int current = low;
while (left <= mid && right <= high) {
if (arr[left] <= arr[right]) {
temp[current++] = arr[left++];
} else {
temp[current++] = arr[right++];
}
}
while (left <= mid) {
temp[current++] = arr[left++];
}
// 这里加上点判断,其实可以省去
while (right <= high) {
temp[current++] = arr[right++];
}
while (low <= high) {
arr[low] = temp[low];
low++;
}
System.out.println(Arrays.toString(arr));
}
}
2、第2版
import java.util.Arrays;
public class MergeSortDemo2 {
public static void main(String[] args) {
int[] arr = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
mergeSort(arr, 0, arr.length - 1);
}
private static void mergeSort(int[] arr, int low, int high) {
// 这里一定是小于号
if (low < high) {
int mid = (low + high) / 2;
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);
merge(arr, low, mid, high);
}
}
private static void merge(int[] arr, int low, int mid, int high) {
int left = low;
int right = mid + 1;
// 这里的temp长度其实可以不用和arr一样
int[] temp = new int[arr.length];
// 将current设为左部分的第一个下标
int current = low;
while (left <= mid && right <= high) {
if (arr[left] <= arr[right]) {
temp[current++] = arr[left++];
} else {
temp[current++] = arr[right++];
}
}
if (left <= mid) {
while (left <= mid) {
temp[current++] = arr[left++];
}
while (low <= high) {
arr[low] = temp[low];
low++;
}
}
// 左部分已经全部复制到temp里,此时右部分从right下标开始之后的元素都>=左部分最大的值,并且有序,所以不用再将right之后的元素从arr复制到temp里,直接将当前的temp往原数组复制即可
if (right <= high) {
while (low <= right - 1) {
arr[low] = temp[low];
low++;
}
}
System.out.println(Arrays.toString(arr));
}
}
3、第3版
import java.util.Arrays;
public class MergeSortDemo3 {
public static void main(String[] args) {
int[] arr = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
mergeSort(arr, 0, arr.length - 1);
}
private static void mergeSort(int[] arr, int low, int high) {
// 这里一定是小于号
if (low < high) {
int mid = (low + high) / 2;
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);
merge(arr, low, mid, high);
}
}
private static void merge(int[] arr, int low, int mid, int high) {
int left = low;
int right = mid + 1;
// temp长度只须等于merge后的部分数组的总长度
int[] temp = new int[high - low + 1];
// 此时current要设为0
int current = 0;
while (left <= mid && right <= high) {
if (arr[left] <= arr[right]) {
temp[current++] = arr[left++];
} else {
temp[current++] = arr[right++];
}
}
while (left <= mid) {
temp[current++] = arr[left++];
}
while (right <= high) {
temp[current++] = arr[right++];
}
// 回存时,要再次将current要设为0
current = 0;
while (low <= high) {
arr[low++] = temp[current++];
}
System.out.println(Arrays.toString(arr));
}
}
4、第4版
import java.util.Arrays;
public class MergeSortDemo4 {
public static void main(String[] args) {
int[] arr = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
mergeSort(arr, 0, arr.length - 1);
}
private static void mergeSort(int[] arr, int low, int high) {
// 这里一定是小于号
if (low < high) {
int mid = (low + high) / 2;
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);
merge(arr, low, mid, high);
}
}
private static void merge(int[] arr, int low, int mid, int high) {
int left = low;
int right = mid + 1;
// temp长度只须等于merge后的部分数组的总长度
int[] temp = new int[high - low + 1];
// 此时current要设为0
int current = 0;
while (left <= mid && right <= high) {
if (arr[left] <= arr[right]) {
temp[current++] = arr[left++];
} else {
temp[current++] = arr[right++];
}
}
if (left <= mid) {
while (left <= mid) {
temp[current++] = arr[left++];
}
// 回存时,要再次将current要设为0
current = 0;
while (low <= high) {
arr[low++] = temp[current++];
}
}
// 左部分已经全部复制到temp里,此时右部分从right下标开始之后的元素都>=左部分最大的值,并且有序,所以不用再将right之后的元素从arr复制到temp里,直接将当前的temp往原数组复制即可
if (right <= high) {
// 回存时,要再次将current要设为0
current = 0;
while (low <= right - 1) {
arr[low++] = temp[current++];
}
}
System.out.println(Arrays.toString(arr));
}
}
5、一些笔记
最基本的调用过程:
一般情况的调用过程:
合并的过程: