-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSort.java
48 lines (42 loc) · 1.26 KB
/
MergeSort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package Sort;
/**
归并排序
时间复杂度 平均o(nlogn) 最好o(nlogn) 最坏o(nlogn)
空间复杂度 o(1)
稳定排序
**/
public class MergeSort {
public static void mergeSort(Integer[] array) {
sort(array, 0, array.length - 1);
}
public static void sort(Integer[] array, int left, int right) {
if (left == right) {
return;
}
int mid = (left + right) >> 1;
sort(array, left, mid);
sort(array, mid + 1, right);
merge(array, left, mid, right);
}
public static void merge(Integer[] array, int left, int mid, int right) {
int[] tmp = new int[right - left + 1];
int index = 0, p1 = left, p2 = mid + 1;
while (p1 <= mid && p2 <= right) {
tmp[index++] = array[p1] < array[p2] ? array[p1++] : array[p2++];
}
while (p1 <= mid) {
tmp[index++] = array[p1++];
}
while (p2 <= mid) {
tmp[index++] = array[p2++];
}
for (int i = 0; i < tmp.length; i++) {
array[left + i] = tmp[i];
}
}
public static void main(String[] args) {
Integer[] c = {100, 4, 9, 23, 1, 45, 27, 5, 2};
mergeSort(c);
Stream.of(c).forEach(System.out::println);
}
}