package com.itheima;
import java.util.Arrays;
/**
* 堆排序(也是一种选择排序)
* 包含了两个重要的方法:1.建堆(大顶堆),2.交换堆顶和最后一个元素的值
* @author Administrator
*
*/
public class HeapSort {
/**
* 堆排序方法的实现
* @param data
*/
public static void headSort(int a[]){
System.out.println("开始排序");
int arrayLength = a.length;
//循环建立大顶堆
for (int i = 0; i < a.length - 1; i++) {
//建堆
buildMaxHeap(a, a.length - i - 1);
//交换堆顶和最后一个元素(数组下标从0开始)
swap(a, 0, arrayLength - i - 1);
System.out.println(Arrays.toString(a));
}
}
/**
* 交换数据
* @param a
* @param i
* @param j
*/
private static void swap(int a[], int i, int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
/**
* 对数组data从0到lastIndex建立大顶堆。
* @param a
* @param lastIndex
*/
private static void buildMaxHeap(int data[], int lastIndex){
//从lastIndex节点处(最后一个节点)的父节点开始:(lastIndex - 1) / 2
for (int i = (lastIndex - 1) / 2; i >= 0; i--) {
//k保存正在判断的节点
int k = i;
//如果当前k点的子节点存在
while ((k * 2 + 1) <= lastIndex) {
// k节点的左子节点的索引
int biggerIndex = 2 * k + 1;
//如果biggerIndex<lastIndex,说明biggerIndex + 1 代表的是k节点的右子节点的索引
if (biggerIndex < lastIndex) {
// biggerIndex + 1 代表的是k节点的右子节点的索引
// 如果右子节点的值比较大
if (data[biggerIndex] < data[biggerIndex + 1]) {
//biggerIndex总是记录的是较大子节点的索引
biggerIndex ++;
}
}
//如果k节点的值小于其较大子节点的值就叫唤他们
if (data[k] < data[biggerIndex]) {
swap(data, k, biggerIndex);
//将biggerIndex赋给k,重新while进行下一次循环,保证k节点的值大于其左右节点的值
k = biggerIndex;
} else {
break;
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[] = {49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
headSort(a);
}
}
运行结果:
开始排序
[4, 98, 65, 97, 76, 64, 62, 49, 78, 34, 34, 35, 53, 51, 27, 49, 38, 54, 56, 17, 18, 23, 12, 15, 13, 25, 5, 99]
[5, 97, 65, 78, 76, 64, 62, 49, 56, 34, 34, 35, 53, 51, 27, 49, 38, 54, 4, 17, 18, 23, 12, 15, 13, 25, 98, 99]
[25, 78, 65, 56, 76, 64, 62, 49, 54, 34, 34, 35, 53, 51, 27, 49, 38, 5, 4, 17, 18, 23, 12, 15, 13, 97, 98, 99]
[13, 76, 65, 56, 34, 64, 62, 49, 54, 25, 34, 35, 53, 51, 27, 49, 38, 5, 4, 17, 18, 23, 12, 15, 78, 97, 98, 99]
[15, 56, 65, 54, 34, 64, 62, 49, 13, 25, 34, 35, 53, 51, 27, 49, 38, 5, 4, 17, 18, 23, 12, 76, 78, 97, 98, 99]
[12, 56, 64, 54, 34, 53, 62, 49, 13, 25, 34, 35, 15, 51, 27, 49, 38, 5, 4, 17, 18, 23, 65, 76, 78, 97, 98, 99]
[23, 56, 62, 54, 34, 53, 51, 49, 13, 25, 34, 35, 15, 12, 27, 49, 38, 5, 4, 17, 18, 64, 65, 76, 78, 97, 98, 99]
[18, 56, 53, 54, 34, 35, 51, 49, 13, 25, 34, 23, 15, 12, 27, 49, 38, 5, 4, 17, 62, 64, 65, 76, 78, 97, 98, 99]
[17, 54, 53, 49, 34, 35, 51, 49, 13, 25, 34, 23, 15, 12, 27, 18, 38, 5, 4, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 49, 53, 49, 34, 35, 51, 38, 13, 25, 34, 23, 15, 12, 27, 18, 17, 5, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[5, 49, 51, 49, 34, 35, 27, 38, 13, 25, 34, 23, 15, 12, 4, 18, 17, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[17, 49, 35, 49, 34, 23, 27, 38, 13, 25, 34, 5, 15, 12, 4, 18, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[17, 49, 35, 38, 34, 23, 27, 18, 13, 25, 34, 5, 15, 12, 4, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 38, 35, 18, 34, 23, 27, 17, 13, 25, 34, 5, 15, 12, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[12, 34, 35, 18, 34, 23, 27, 17, 13, 25, 4, 5, 15, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[15, 34, 27, 18, 34, 23, 12, 17, 13, 25, 4, 5, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[5, 34, 27, 18, 25, 23, 12, 17, 13, 15, 4, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 25, 27, 18, 15, 23, 12, 17, 13, 5, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[5, 25, 23, 18, 15, 4, 12, 17, 13, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[13, 18, 23, 17, 15, 4, 12, 5, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[5, 18, 13, 17, 15, 4, 12, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[12, 17, 13, 5, 15, 4, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 15, 13, 5, 12, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 12, 13, 5, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[5, 12, 4, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]