当前位置:网站首页>The creation of a large root heap (video explanation)
The creation of a large root heap (video explanation)
2022-07-30 09:53:00 【Learning to pursue high efficiency】
大根堆的创建2
注意:
- In fact, the left and right nodes They are all going to find the parent node,than when traversing,Just traverse through the parent node(The number of traversals can be reduced as well The number of loops in the traversal pass)
for (int parent = (usedSize-1-1) / 2; parent >= 0 ; parent--)
public class TestHeap {
public int[] elem;
public int usedSize;//The number of valid elements in the current heap
public TestHeap() {
this.elem = new int[10];
this.usedSize = 0;
}
public void initArray(int[] array) {
elem = Arrays.copyOf(array,array.length);
usedSize = elem.length;
}
/** * 建堆:【大根堆】 * 时间复杂度:O(n) */
public void createHeap() {
for (int parent = (usedSize-1-1) / 2; parent >= 0 ; parent--) {
shiftDown(parent,usedSize);
}
}
/** * 实现 向下调整 * @param parent The subscript of the root node of each subtree * @param len 每棵子树的结束位置 */
private void shiftDown(int parent ,int len) {
int child = 2 * parent + 1;
//At least there are left children
while (child < len) {
//判断 左孩子 和 右孩子 谁最大,前提是 必须有 右孩子
if(child+1 < len && elem[child] < elem[child+1]) {
child++;//此时 The subscript of the maximum value is stored
}
if(elem[child] > elem[parent]) {
swap(elem,child,parent);
parent = child;
child = 2*parent+1;
}else {
break;
}
}
}
private void swap(int[] array,int i,int j) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
边栏推荐
- Unity performance analysis Unity Profile performance analysis tool
- 实战演练 | 在 MySQL 中计算每日平均日期或时间间隔
- 虚幻引擎图文笔记:could not be compiled. Try rebuilding from source manually.问题的解决
- XP电源维修fleXPower电源X7-2J2J2P-120018系列详解
- leetcode 剑指 Offer 46. 把数字翻译成字符串
- C# 之 $ – 字符串内插
- Oracle 创建和操作表
- PyQt5-绘制不同类型的直线
- PyTorch安装及环境配置(Win10)
- 利用R语言读取csv文件入一个数据框,然后查看各列的属性。
猜你喜欢

Using IN in MySQL will not go through index analysis and solutions

快解析结合友加畅捷通t1飞跃版

仿牛客网项目第二章:开发社区登录模块(详细步骤和思路)

leetcode 剑指 Offer 42. 连续子数组的最大和

Devops和低代码的故事:螳螂捕蝉,黄雀在后

Unified exception handling causes ResponseBodyAdvice to fail

Kotlin value class - value class

Matplotlib--绘图标记

LeetCode二叉树系列——94.二叉树的中序遍历

图像分析:投影曲线的波峰查找
随机推荐
PyQt5快速开发与实战 7.4 事件处理机制入门 and 7.5 窗口数据传递
ospf2双点双向重发布(题2)
Concise Notes on Integrals - Types of Curve Integrals of the First Kind
Concise Notes on Integrals - Types of Curve Integrals of the Second Kind
(***重点***)Flink常见内存问题及调优指南(一)
Detailed description of iperf3 parameter options
Unable to locate the program input point ucrtbase.abort on the dynamic link library api-ms-win-crt-runtime-|1-1-0.dll
快解析结合用友时空
ClickHouse
2022 Hangzhou Electric Multi-School 1st Game
ThreadLocal内存泄漏是伪命题?
微软 SQL 服务器被黑,带宽遭到破坏
嘉为鲸翼·多云管理平台荣获信通院可信云技术服务最佳实践
转行软件测试,报培训班3个月出来就是高薪工作,靠谱吗?
虚幻引擎图文笔记:could not be compiled. Try rebuilding from source manually.问题的解决
Excel xlsx file not supported两种解决办法【杭州多测师】【杭州多测师_王sir】
编译报错: undefined reference to `google::FlagRegisterer::FlagRegisterer解决方法
2022 Hangzhou Electric Multi-School 2nd Game
函数式接口&Lambda表达式——简单应用笔记
快解析结合任我行crm