-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge.java
More file actions
47 lines (41 loc) · 1.07 KB
/
Merge.java
File metadata and controls
47 lines (41 loc) · 1.07 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
package chapter2.sort;
/***
* 归并排序
*/
public class Merge extends Action
{
private static Double[] aux; // 归并所需要的辅助数组
public static void sort(Double[] a)
{
aux = new Double[a.length]; // 一次性分配空间
sort(a, 0, a.length-1);
}
private static void sort(Double[] a, int lo, int hi)
{
//将数组a[lo, hi]排序
if(hi <= lo) return;
int mid = lo + (hi-lo)/2;
sort(a, lo, mid); //将左半部分排序
sort(a, mid+1, hi); // 将右半部分排序
merge(a,lo,mid,hi);
}
public static void merge(Double[] a, int lo, int mid, int hi)
{
int i = lo, j = mid+1;
for(int k = lo; k <= hi; k++)
{
aux[k] = a[k];
}
for (int k = lo; k <= hi ; k++)
{
if(i>mid)
a[k] = aux[j++];
else if(j>hi)
a[k] = aux[i++];
else if(less(aux[j], aux[i]))
a[k] = aux[j++];
else
a[k] = aux[i++];
}
}
}