排序算法 - 堆排序

排序算法 - 堆排序


思路

动图演示

代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void sort(int[] arr) {
int len = arr.length;
int step = len/2;
while (step > 0) {
for (int i = 0; i < step; i++) {
for (int j = i + step; j < len; j += step) {
int n = j;
while (n - step >= 0) {
if (arr[n] >= arr[n - step]) break;
int temp = arr[n];
arr[n] = arr[n - step];
arr[n - step] = temp;
n -= step;
}
}
}
step /= 2;
}
}