-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeBU.java
More file actions
40 lines (37 loc) · 1.01 KB
/
MergeBU.java
File metadata and controls
40 lines (37 loc) · 1.01 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
package com.mytest.algorithm.sort;
import com.mytest.algorithm.util.Util;
/**
*
* @author wangyujiang
* 自顶向上的归并排序
* 多次遍历整个数组,根据整个数组大小进行两两归并
*/
public class MergeBU {
//原地归并
public static void merge(Comparable[] a, int lo, int mid, int hi) {
int i = lo, j = mid+1;
Comparable[] aux = new Comparable[a.length];
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(Util.less(aux[i], aux[j])) {
a[k] = aux[i++];
} else{
a[k] = aux[j++];
}
}
}
public static void sort(Comparable[] a){
int N = a.length;
for(int sz = 1; sz < N; sz = sz+sz) {//进行每一轮归并,下一轮子数组的大小会翻倍
for(int lo = 0; lo < N-sz; lo += sz+sz) { //确定归并子数组lo的位置
merge(a, lo, lo+sz-1, lo+sz+sz-1);//进行归并
}
}
}
}