forked from Jossc/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerging.java
More file actions
62 lines (57 loc) · 1.38 KB
/
Merging.java
File metadata and controls
62 lines (57 loc) · 1.38 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
55
56
57
58
59
60
61
62
package com.algorithm;
/**
* @ClassName Merging
* @Author chenzhuo
* @Version 1.0
* @Date 2019-08-17 14:39
**/
public class Merging {
/**
* 排序
*
* @param array 数字
* @param low 低
* @param high 高
* @return
*/
public static int[] sort(int[] array, int low, int high) {
int mind = (low + high) / 2;
if (low < high) {
sort(array, low, mind);
sort(array, mind + 1, high);
}
return array;
}
/**
* 合并
*
* @param array 数组
* @param low 最低
* @param high 最高
*/
public static void merge(int[] array, 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 (array[i] <= array[j]) {
temp[k++] = array[i++];
} else {
temp[k++] = array[j++];
}
}
// 把左边剩余的数移入数组
while (i <= mid) {
temp[k++] = array[i++];
}
// 把右边边剩余的数移入数组
while (j <= high) {
temp[k++] = array[j++];
}
// 把新数组中的数覆盖nums数组
for (int x = 0; x < temp.length; x++) {
array[x + low] = temp[x];
}
}
}