Java实现归并排序
package Sort; import java.util.Arrays; public class MergeSort { public static void merge(int[] list, int low, int mid, int high) { int[] temp = new int[high - low + 1]; int i = low; int j = mid + 1; int k = 0; while (i <= mid && j <= high) { if (list[i] < list[j]) { temp[k++] = list[i++]; } else { temp[k++] = list[j++]; } } while (i <= mid) { temp[k++] = list[i++]; } while (j <= high) { temp[k++] = list[j++]; } for (int k2 = 0; k2 < temp.length; k2++) { list[k2 + low] = temp[k2]; } } public static int[] sort(int[] list, int low, int high) { int mid = (low + high) / 2; if (low < high) { sort(list, low, mid); sort(list, mid + 1, high); merge(list, low, mid, high); } return list; } public static void main(String[] args) { int[] list = {5,4,3,2,1,6,8,7}; System.out.println(Arrays.toString(sort(list,0,list.length-1))); } }