当前位置:网站首页>堆排序总结
堆排序总结
2022-07-20 15:57:00 【十三豆啊】
前言
堆结构算是一种比较有难度的排序算法,如果大家看了这篇文章理解不了的话建议看一下视频,有些东西可能我描述的不是那么清楚。
1. 定义
堆排序(Heapsort )是利用堆这种数据结构所设计的一种排序算法。
2.堆的定义
堆是具有以下性质的完全二叉树∶
ⅰ. 每个结点的值都大于或等于其左右孩子结点的值,称为大顶堆;
ⅱ. 每个结点的值都小于或等于其左右孩子结点的值,称为小顶堆。
以大顶堆为例,下图是大顶堆的图
我们对堆中的结点按层进行编号,将这种逻辑结构映射到数组中就是下面这个样子:
0 | 95 |
---|---|
1 | 94 |
2 | 66 |
3 | 75 |
4 | 63 |
5 | 33 |
6 | 22 |
7 | 55 |
8 | 44 |
9 | 11 |
从上面的图中,我们可以总结出规律
对于某1个节点i,它的父节点(i-1)/2
对于某1个节点i,它的左孩子2i+1
对于某1个节点i,它的右孩子2i+2
最后一个非叶子节点为(arr.length/2-1)(忽略小数点)
3.堆排序的思路
以大顶堆为例
(1)根据初始数组去构造初始堆(可以看成一个完全二叉树)。
(2)每次交换第一个和最后一个元素,输出最后一个元素(最大值),然后把剩下元素重新调整为大根堆,再进行交换第一个元素和最后一个元素,再调整大顶堆,重复执行,直到整个数组排序完成。
4.代码实现(大顶堆为例)
import java.util.Arrays;
public class Heapsort {
public static void main(String[] args) {
int[] arr = {3,2,1,4,6,77,22,11,22};
heapsort(arr);
System.out.println(Arrays.toString(arr));
}
public static void heapsort(int[] arr){
//建初始堆
for (int i = arr.length/2-1;i >= 0; i--){
heapify(arr, i, arr.length-1);
}
//堆排序的过程
for (int i = arr.length - 1; i > 0; i--){
swap(arr,0,i);
heapify(arr,0,i-1);
}
}
//把一个非堆结构变成堆结构
public static void heapify(int[] arr, int i, int last_index){
int max = i;
if (2*i+1 <= last_index && arr[max] < arr[2*i+1]){
//记录最大值节点
max = 2*i+1;
}
if (2*i+2 <= last_index && arr[max] < arr[2*i+2]){
//记录最大值节点
max = 2*i+2;
}
if (max != i){
swap(arr,i,max);
heapify(arr, max, last_index);
}
}
//交换
public static void swap(int[] arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
5.算法特点
算法稳定性:不稳定,因为在堆顶和堆底交换的时候两个相等的记录在序列的相对位置可能发生变化。
时间复杂度:O(n+nlog2^n) - O(nlog2^n)
边栏推荐
- Transmission 下载列表、下载文件 迁移机器指南
- MDClub 轻量级网论坛源码
- 【微信小程序】 XXXXX不在以下 Socket 合法域名列表中,请参考文档
- Heavy forecast! Analysys, together with Microsoft and the Central University of Finance and economics, talks about the digital economy
- flv.js的追帧、断流重连及实时更新的直播优化方案
- 北森招股书:赛道优势凸显,一体化+中大客户是加分项
- Machine learning - detailed derivation of support vector machine theory (including explanation of examples) (III)
- (‘You must install pydot (`pip install pydot`) and install graphviz...)
- 单片机外部中断触发方式:电平触发和边沿触发两者说明
- codeforces educational round 131 ABCDEF
猜你喜欢
50个名额限量开放|带着OceanBase年度发布会的消息走来了!
91.(leaflet篇)leaflet态势标绘-进攻方向绘制
MySQL ON DELETE CASCADE(级联删除)[猿教程]
根据两个向量计算它们之间的旋转矩阵
Hide & seek introduction -- end-to-end simulation and processing of radio observation data (I)
[vs2019] create C project
[STM32] interrupt (NVIC)
The seemingly simple input box input is abnormally stuck. Remember a troubleshooting idea for daily performance problems
2.负载测试
用户体验 | 深耕用户体验筑造银行竞争的护城河
随机推荐
2022 Niuke summer school first adji
业务出海,灵感乍现前要先「把手弄脏」
Kalman filter meets deep learning: papers related to Kalman filter and deep learning
修改word文档中已有的批注者名称
Bing 必应突然不能用了(2021 年 17 日最新情况),怎么办?问题已解决
关于字符串的分割问题
wine 微信初始化96%卡住
redis 5种数据结构的使方法
codeforces educational round 131 ABCDEF
2.负载测试
ICCV2021 频域图像翻译 Frequency Domain Image Translation: More Photo-realistic, Better Identity-preserving
一、MFC介绍
The k-th smallest array sum in an ordered matrix
2022牛客暑假多校第一场ADJI
在 Excel 内使用 ODBC 消费 SAP ABAP CDS view
什么是Pygame
在VB6 处理pdf 和jpg文件
百度网盘 yundetectservice.exe可以禁用关闭吗
【Bug解决】VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences
[bug resolution] visibledeprecationwarning: creating an ndarray from ragged needed sequences