Java实现归并排序

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、一些笔记


最基本的调用过程:



一般情况的调用过程:



合并的过程:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值