public class HeapOperate2 {
/*
* 建立堆时只需要保证根结点小于两个子结点或者大于两个子结点,对两个子结点大小没有要求
*/
public static void main(String[] args) {
int[] a = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3 };
//在堆排序之前,打印初始数组
print(a);
//进行堆排序
heapSort(a);
//进行堆排序之后
print(a);
}
public static void heapSort(int[] a){
//建立大根堆
buildMaxHeap(a);
for (int i = a.length - 1; i >= 1; i--) {
swap(a[0], a[i]);
maxHeap(a, i, 0);
}
}
private static void buildMaxHeap(int[] a) {
int half = a.length/2;
for (int i = half; i >= 0; i--) {
maxHeap(a, a.length, i);
}
}
private static void maxHeap(int[] a, int heapSize, int index) {
//找出index位置处左右孩子的位置left和right
int left = index * 2 + 1;
int right = index * 2 + 2;
int largest = index;
if (left < heapSize && a[left] > a[index]) {
largest = left;
}
if (right < heapSize && a[right] > a[largest]) {
largest = right;
}
if (index != largest){
//交换两个数据
swap(a[index], a[largest]);
maxHeap(a, heapSize, largest);
}
}
}
b.将堆顶元素与末尾元素交换,将最大元素"沉"到数组末端;
c.重新调整结构,使其满足堆定义,然后继续交换堆顶元素与当前末尾元素,反复执行调整+交换步骤,直到整个序列有序