当前位置:网站首页>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;
}
}
边栏推荐
- 转行软件测试,报培训班3个月出来就是高薪工作,靠谱吗?
- echart图表清空上一次数据
- Integral Special Notes - Definition of Integral
- 虚幻引擎图文笔记:could not be compiled. Try rebuilding from source manually.问题的解决
- 仿牛客网项目第二章:开发社区登录模块(详细步骤和思路)
- 一个低级错误导致的诡异现象——走近科学能拍三集,(C语言)最简单的数组元素读取,不正确!?
- CSDN21天学习挑战赛
- Explain the problem of change exchange in simple terms - the shell of the backpack problem
- 日志导致线程Block的这些坑,你不得不防
- 2022 Hangzhou Electric Multi-School 1st Game
猜你喜欢

日志导致线程Block的这些坑,你不得不防
容器技术 -- 简单了解 Kubernetes 的对象

The use of qsort function and its analog implementation

MySQL Explain usage and parameter detailed explanation

包、类及四大权限和static

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

微软 SQL 服务器被黑,带宽遭到破坏

leetcode 剑指 Offer 46. 把数字翻译成字符串

HCIP --- MPLS VPN实验

Circuit analysis: constant current source circuit composed of op amp and triode
随机推荐
Unable to locate the program input point ucrtbase.abort on the dynamic link library api-ms-win-crt-runtime-|1-1-0.dll
Concise Notes on Integrals - Types of Curve Integrals of the Second Kind
leetcode 剑指 Offer 57. 和为s的两个数字
学习笔记10--局部轨迹生成主要方法
百度推广助手遇到重复关键字,验证错误,怎么一键删除多余的
快解析结合象过河erp
微软 SQL 服务器被黑,带宽遭到破坏
【无标题】
leetcode 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
Using IN in MySQL will not go through index analysis and solutions
Kotlin value class - value class
2022/07/29 Study Notes (day19) Exception Handling
PyQt5-在窗口上绘制文本
Oracle 创建和操作表
els 方块停在方块上。
实战演练 | 在 MySQL 中计算每日平均日期或时间间隔
详解JVM垃圾回收
怎么在本地电脑上运行dist文件
HCIP - MPLS VPN experiment
一文理解分布式开发中的服务治理