## 归并排序算法Java实现

 public class ArrayUtils {

public static void printArray(int[] array) {
System.out.print("{");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
if (i < array.length - 1) {
System.out.print(", ");
}
}
System.out.println("}");
}
}

 public class MergeSort {

public static void main(String[] args) {
int[] array = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3 };

System.out.println("Before sort:");
ArrayUtils.printArray(array);

mergeSort(array);

System.out.println("After sort:");
ArrayUtils.printArray(array);
}

public static void mergeSort(int[] array) {
sortArray(array, 0, array.length - 1);
}

private static void sortArray(int[] array, int start, int end) {
// 单个元素不需要排序,直接返回
if (start == end) {
return;
}

int sortSize = end - start + 1;
int seperate;
if (sortSize % 2 == 0) {
seperate = start + sortSize / 2 - 1;
} else {
seperate = start + sortSize / 2;
}

sortArray(array, start, seperate);
sortArray(array, seperate + 1, end);

mergeArray(array, start, seperate, end);
}

private static void mergeArray(int[] array, int start, int seperate, int end) {
int totalSize = end - start + 1;
int size1 = seperate - start + 1;
int size2 = end - seperate;

int[] array1 = new int[size1];
int[] array2 = new int[size2];

for (int i = 0; i < size1; i++) {
array1[i] = array[start + i];
}

for (int i = 0; i < size2; i++) {
array2[i] = array[seperate + 1 + i];
}

int mergeCount = 0;
int index1 = 0;
int index2 = 0;

while (mergeCount < totalSize) {
// 先检查有没有其中的一个数组已经处理完
if (index1 == size1) {
for (int i = index2; i < size2; i++) {
array[start + mergeCount] = array2[i];
mergeCount++;
index2++;
}
} else if (index2 == size2) {
for (int i = index1; i < size1; i++) {
array[start + mergeCount] = array1[i];
mergeCount++;
index1++;
}
} else {
int value1 = array1[index1];
int value2 = array2[index2];

if (value1 == value2) {
array[start + mergeCount] = value1;
array[start + mergeCount + 1] = value1;
mergeCount += 2;
index1++;
index2++;
} else if (value1 < value2) {
array[start + mergeCount] = value1;
mergeCount++;
index1++;
} else if (value1 > value2) {
array[start + mergeCount] = value2;
mergeCount++;
index2++;
}
}
}
}
}

#### Merge Sort的java语言实现

2015-05-31 15:49:23

#### Java练习：分治法之合并排序(merge Sort)

2016-06-09 17:59:10

#### Java排序算法(三)--归并排序（MergeSort）递归与非递归的实现

2016-03-21 09:39:22

#### 归并排序Merge Sort LinkedList in Java

2015-08-25 10:38:32

#### java实现Merge Intervals

2014-07-02 20:04:45

#### （五）RxJava操作符：merge合并操作符

2016-03-29 15:47:28

#### 【排序算法】归并排序原理及Java实现

2016-04-27 17:35:43

#### 【Java常用排序算法】归并排序（二路归并排序）

2017-02-27 23:17:10

#### 排序算法Java实现——归并排序

2017-04-25 22:09:04

#### 排序算法之归并排序(JAVA)

2012-12-28 10:59:36