-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathHeapSort.java
54 lines (45 loc) · 1.31 KB
/
HeapSort.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
49
50
51
52
53
54
package heap;
import interf.SortImpl;
/**
* 堆排序
* (大顶堆 / 基于递归)
*/
public class HeapSort extends SortImpl {
@Override
public void sort(int[] arr) {
if (null == arr || 1 >= arr.length) return;
int boundary = arr.length - 1;
build(arr); // 构建初始大顶堆
swap(arr, 0, boundary); // 保存当前堆顶到末尾
while (--boundary >= 0) {
adjust(arr, 0, boundary); // 调整每个子堆为大顶堆
swap(arr, 0, boundary); // 保存当前堆顶到末尾
}
}
/**
* 构建原始大顶堆
*/
private void build(int[] arr) {
int n = arr.length / 2; // 从最后一个非叶子节点开始
for (int i = n - 1; i >= 0 ; i--) {
adjust(arr, i, arr.length - 1);
}
}
/**
* 调整子顶堆为大顶堆
*/
private void adjust(int[] arr, int top, int boundary) {
fix(arr, top, 2 * top + 1, boundary);
fix(arr, top, 2 * top + 2, boundary);
}
/**
* 与子节点比较,并调整子顶堆为大顶堆
*/
private void fix(int[] arr, int top, int sub, int boundary) {
if (sub > boundary) return;
if (arr[top] < arr[sub]) {
swap(arr, top, sub);
adjust(arr, sub, boundary);
}
}
}