33,006
社区成员
发帖
与我相关
我的任务
分享
public class HeapSort_a {
private static void adjust(int H[], int s, int length) {
int tmp = H[s];
int child = 2 * s + 1;
while (child < length) {
if (child + 1 < length && H[child] < H[child + 1]) {
++child;
}
if (H[s] < H[child]) {
H[s] = H[child];
s = child;
child = 2 * s + 1;
} else {
break;
}
H[s] = tmp;
}
}
private static void build(int H[], int length) {
for (int i = (length - 1) / 2; i > 0; i--) {
adjust(H, i, length);
}
}
public static void sort(int H[], int length) {
if (H.length > 0) {
build(H, length);
for (int i = length - 1; i >= 0; --i) {
int tmp = H[i];
H[i] = H[0];
H[0] = tmp;
adjust(H, 0, i);
}
}
}
public static void main(String[] args)
{
int[] A = {1,3,4};
HeapSort_a.sort(A,A.length);
for(int i = 0;i<A.length;i++)
{
System.out.println(A[i]);
}
}
}