当前位置:网站首页>ACM tutorial - heap sorting
ACM tutorial - heap sorting
2022-06-11 01:58:00 【Sheep yard】
Definition
Heap is a data structure called complete binary tree . Here we use two types of piles , In fact, it's a kind of .
- Big pile top : The value of each node is greater than or equal to the value of its left and right child nodes .
- Small cap pile : The value of each node is less than or equal to the value of its left and right child nodes .
Ps: Small top stack error , node 3 and node 2 Change position !
As shown above , There are two kinds of piles . If we map this logical structure to Array in , It's just like this below
| 9 | 5 | 8 | 2 | 3 | 4 | 7 | 1 |
| 1 | 2 | 5 | 4 | 3 | 8 | 9 | 7 |
This array arr Logically, it's a heap . From here we can draw the following properties ( a key )
- For the big top heap :arr[i] >= arr[2i + 1] && arr[i] >= arr[2i + 2]
- For a small top pile :arr[i] <= arr[2i + 1] && arr[i] <= arr[2i + 2]
- stability : according to Equivalent element In the array Relative order Is it changed , Sorting algorithms can be divided into 「 Stable sequencing 」 and 「 Unstable ordering 」 Two types of .
- Locality : According to the sorting process Whether to use additional memory ( Auxiliary array ), Sorting algorithms can be divided into 「 In situ sorting 」 and 「 Offsite sorting 」 Two types of . In a general way , Since no external memory is used , Sorting in place is more efficient than sorting not in place .
- Self adaptability : According to the algorithm Time complexity whether Affected by the element distribution of the array to be sorted , Sorting algorithms can be divided into 「 Adaptive sorting 」 and 「 Non adaptive sorting 」 Two types of .「 Adaptive sorting 」 The time complexity of is affected by the element distribution , On the contrary, it will not be affected .
- Comparative : The sort of comparison classes is based on Comparison operator ( Less than 、 equal 、 Greater than ) To determine the relative order of elements ; Relative , Non comparison sorting is not based on comparison operator .
The illustration

| 4 | 5 | 8 | 2 | 3 | 9 | 7 | 1 |
Take this array as an example , Build the largest heap to sort from small to large , The main points are as follows
- Build the maximum heap at the beginning , From the last non leaf node to the root node ( From bottom to top , From right to left )
- When it's your turn ( Currently viewed as a parent node ), Compare your left child node with your right child node , Finally, the largest node is determined , If the largest node is itself , Don't swap ; Otherwise, you can exchange your own node with the largest node
- If the first 2 There is no exchange at the point ( Skip the first 3 spot ), Otherwise, continue to replace the child nodes ( It was originally the parent node , That's right. ), Continue to maintain the same comparison operation , Until there are no nodes

- The above maximum heap construction is completed , Next, start the real heap sort , First build complete , The root node must be the maximum , Change to the last 1 A leaf node , At the end of the day 1 Nodes to the root node , Then maintain the heap , Then the second 2 A maximum of , Then switch the root node to the penultimate node again 2 A leaf node , And so on . complete ( Because the space is limited , Only the key intermediate process legends in the explanation are listed )




nature
- Time complexity
- The best O(nlogn)
- Average O(nlogn)
- The worst O(nlogn)
- Spatial complexity
- The worst O(1)
- stability : unstable
- Locality : In situ
- Self adaptability : The adaptive
- Comparative : Compare
Java
public class HeapSort {
// The entrance is here
public static void heapSort(int[] arr) {
if (arr == null || arr.length == 0) {
return;
}
int len = arr.length;
// Build the big top heap , This is actually the sequence to be sorted , Into an array of large top heap structures
buildMaxHeap(arr, len);
// Swap nodes at the top of the heap and at the current end , Reset the big top stack
for (int i = len - 1; i > 0; i--) {
swap(arr, 0, i);
len--;
heapify(arr, 0, len);
}
}
private static void buildMaxHeap(int[] arr, int len) {
// Traverse forward from the last non leaf node , Adjust node properties , Make it a big top pile
for (int i = (len - 1) / 2; i >= 0; i--) {
heapify(arr, i, len);
}
}
private static void heapify(int[] arr, int i, int len) {
// First, according to the nature of the heap , Find the index of its left and right nodes
int left = 2 * i + 1;
int right = 2 * i + 2;
// The default is the current node ( Parent node ) Is the maximum value. .
int largestIndex = i;
if (left < len && arr[left] > arr[largestIndex]) {
// If there is a left node , And the value of the left node is larger , Update the index of the maximum value
largestIndex = left;
}
if (right < len && arr[right] > arr[largestIndex]) {
// If there is a right node , And the value of the right node is greater , Update the index of the maximum value
largestIndex = right;
}
if (largestIndex != i) {
// If the maximum value is not the value of the current non leaf node , Then swap the values of the current node and the child node of the maximum value
swap(arr, i, largestIndex);
// Because after the exchange , The value of the child node changes , If the child node also has its own child node , It still needs to be adjusted again .
heapify(arr, largestIndex, len);
}
}
private static void swap (int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}边栏推荐
- 【音乐】基于matlab演奏《青花瓷》【含Matlab源码 1873期】
- [matlab] image restoration
- 【图像处理】基于matlab GUI多功能图像处理系统【含Matlab源码 1876期】
- flutter 状态管理
- 2021-7-18 ROS notes XML language related
- Leetcode 430 flat a multilevel double linked list (DFS linked list)
- The argument type ‘int?‘ can‘t be assigned to the parameter type ‘num‘
- 【BSP视频教程】BSP视频教程第17期:单片机bootloader专题,启动,跳转配置和调试下载的各种用法(2022-06-10)
- LeetCode 1024 Video Stitching (dp,jump game)
- 从解读 BDC 自动生成的代码谈起,讲解 SAPGUI 的程序组成部分试读版
猜你喜欢

Metersphere tutorial: how to assert when the returned result of the interface is null
![[leetcode] restore binary search tree](/img/92/14c4d670f318f93297040241a61c41.jpg)
[leetcode] restore binary search tree

Today's sleep quality record 80 points

Leetcode linked list queue stack problem
![[error record] Android application security detection vulnerability repair (strandhogg vulnerability | set activity component android:taskaffinity=](/img/42/2310fa6a1563082d4ce2a1906eba44.png)
[error record] Android application security detection vulnerability repair (strandhogg vulnerability | set activity component android:taskaffinity= "")

flutter 状态管理

Leetcode 430 flat a multilevel double linked list (DFS linked list)

逻辑漏洞 / 业务漏洞

Flutter status management

Win11触摸键盘主题如何更换?Win11更换触摸键盘主题的方法
随机推荐
小包子关于分红的思考
[matlab] image compression coding (DCT, RLE)
2021-07-18 ROS笔记-基础和通讯
Project sorting of Online Exercise System Based on gin and Gorm
[leetcode] restore binary search tree
【MATLAB】MATLAB图像处理基本操作
Leetcode 1574 shortest subarray to be removed to make array sorted
2021-2-26 compilation of programming language knowledge points
Xpath注入
薪的测试开发程序员们,你为何要走?和产品相互残杀到天昏地暗......
并发编程基础底层原理学习(四)
【圖像處理】基於matlab GUI多功能圖像處理系統【含Matlab源碼 1876期】
【错误记录】Android 应用安全检测漏洞修复 ( StrandHogg 漏洞 | 设置 Activity 组件 android:taskAffinity=““ )
MATLAB数组其他常见操作笔记
关于概率统计中的排列组合
MATLAB数字运算函数笔记
Leetcode 2171 removing minimum number of magic beans (prefix and recommendation)
"It looks like robbing tickets but actually robbing money". Don't be fooled by fancy ticket robbing products again and again
Leetcode 430 flat a multilevel double linked list (DFS linked list)
China-open-ssl编译的一些记录