-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest29.java
More file actions
54 lines (49 loc) · 1.35 KB
/
test29.java
File metadata and controls
54 lines (49 loc) · 1.35 KB
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
49
50
51
52
53
54
package byteDance;
/**
* Created by lizeyang on 2020/5/15.
* 归并排序,so easy~
*/
public class test29 {
//时间复杂度O(nlogn),稳定
public static int[] sort(int[] nums) {
if (nums == null || nums.length == 0) {
return nums;
}
mergeSort(nums, 0, nums.length - 1);
return nums;
}
public static void mergeSort(int[] nums, int l, int r) {
if (l >= r) {
return;
}
int mid = (l + r) >> 1;
mergeSort(nums, l, mid);
mergeSort(nums, mid + 1, r);
partition(nums, l, mid, r);
}
public static void partition(int[] nums, int l, int mid, int r) {
int[] help = new int[r - l + 1];
int p = l;
int q = mid + 1;
int index = 0;
while (p <= mid && q <= r) {
help[index++] = nums[p] < nums[q] ? nums[p++] : nums[q++];
}
while (p <= mid) {
help[index++] = nums[p++];
}
while (q <= r) {
help[index++] = nums[q++];
}
for (int i = 0; i < help.length; i++) {
nums[l + i] = help[i++];
}
}
public static void main(String[] args) {
int[] nums = {1, 3, 4, 6, 2, 8, 5};
int[] res = sort(nums);
for (int i = 0; i < res.length; i++) {
System.out.print(res[i] + " ");
}
}
}