public class HeapSort
{
public static void main(String[] args)
{
int[] arr = { 50, 10, 90, 30, 70, 40, 80, 60, 20 };
System.out.println("排序之前:");
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
// 堆排序
heapSort(arr);
System.out.println();
System.out.println("排序之后:");
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
}
/**
* 堆排序
*/
private static void heapSort(int[] arr)
{
// 将待排序的序列构建成一个大顶堆
for (int i = arr.length / 2; i >= 0; i--)
heapAdjust(arr, i, arr.length);
// 逐步将每个最大值的根节点与末尾元素交换,并且再调整二叉树,使其成为大顶堆
for (int i = arr.length - 1; i > 0; i--)
{
swap(arr, 0, i); // 将堆顶记录和当前未经排序子序列的最后一个记录交换
heapAdjust(arr, 0, i); // 交换之后,需要重新检查堆是否符合大顶堆,不符合则要调整
}
}
/**
* 构建堆的过程
* @param arr 需要排序的数组
* @param i 需要构建堆的根节点的序号
* @param n 数组的长度
*/
private static void heapAdjust(int[] arr, int i, int n)
{
int child;
int father;
for (father = arr[i]; leftChild(i) < n; i = child)
{
child = leftChild(i);
// 如果左子树小于右子树,则需要比较右子树和父节点
if (child != n - 1 && arr[child] < arr[child + 1])
child++; // 序号增1,指向右子树
// 如果父节点小于孩子结点,则需要交换
if (father < arr[child])
arr[i] = arr[child];
else
break; // 大顶堆结构未被破坏,不需要调整
}
arr[i] = father;
}
// 获取到左孩子结点
private static int leftChild(int i)
{
return 2 * i + 1;
}
// 交换元素位置
private static void swap(int[] arr, int index1, int index2)
{
int tmp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = tmp;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有