当前位置:网站首页>Eight sorts ------------- heap sort
Eight sorts ------------- heap sort
2022-07-29 06:18:00 【Plum juice】
Heap sort :
1. Build the big top heap
How to build a large top pile First, traverse the query for each value of the array to see if there is a corresponding child node , Then determine whether the left child node is larger than the right child node , If it is less than , Let the left child node ++, Then exchange the child node and the parent node to traverse , In the end =0, Then let its child node be the parent node , The child node of a child is the child node , Compared with children when changing nodes laugh , In this way, a lot of roofs are built
2. Swap the top element with the last value , then length-1 Continue to traverse upward to determine a maximum value each time .
public static void main(String[] args) {
int[] arr = new int[]{5,7,4,2,0,3,1,6};
for (int p = arr.length-1;p>=0;p--){
adjust(arr,p,arr.length);
}
// Top and bottom elements are interchanged
for(int i = arr.length-1;i>=0;i--){
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
adjust(arr,0,i);
}
System.out.println(Arrays.toString(arr));
}
public static void adjust(int[] arr,int parent,int length) {
int Child = 2 * parent + 1;
while(Child<length) {
int rChild = Child+1;
if(rChild<length && arr[rChild]>arr[Child]) {
Child++;
}
if(arr[parent]<arr[Child]) {
int temp = arr[Child];
arr[Child] = arr[parent];
arr[parent] = temp;
parent = Child;
Child = 2 * Child + 1;
}else {
break;
}
}
}
边栏推荐
- Power electronics: single inverter design (matlab program +ad schematic diagram)
- Design and implementation of QT learning notes data management system
- 【软件工程之美 - 专栏笔记】28 | 软件工程师的核心竞争力是什么?(下)
- Joiner.on和stream().map联合使用技巧
- Si12T和Si14T低功耗电容触摸芯片
- 基于DAC0832的直流电机控制系统
- 【软件工程之美 - 专栏笔记】21 | 架构设计:普通程序员也能实现复杂系统?
- STM32 MDK(Keil5) Contents mismatch错误总结
- QT learning notes QtSql
- 【软件工程之美 - 专栏笔记】30 | 用好源代码管理工具,让你的协作更高效
猜你喜欢
随机推荐
STM32FF030 替代国产单片机——DP32G030
倾角传感器用于通信铁塔、高压电塔长期监测
循环链表和双向链表
基于F407ZGT6的WS2812B彩灯驱动
2022 spring recruit - Hesai technology FPGA technology post (one or two sides, collected from: Digital IC workers and FPGA Explorers)
Abstract classes and interfaces
八大排序-----------------堆排序
基于51单片机的DAC0832波形发生器
STM32 MDK(Keil5) Contents mismatch错误总结
【RoboMaster】从零开始控制RM电机(2)-CAN通信原理及电调通信协议
网络安全学习篇
顺序表和链表
scanBasePackages扫包范围配置
SimpleFOC调参1-力矩控制
LeetCode #35.搜索插入位置
Hal library learning notes-12 SPI
JUC并发知识点
synchronized八锁现象理解
Hal library learning notes-14 ADC and DAC
【软件工程之美 - 专栏笔记】21 | 架构设计:普通程序员也能实现复杂系统?









