当前位置:网站首页>Heap Sort
Heap Sort
2022-08-04 10:33:00 【51CTO】
Heap Sort is an improvement to direct selection sort.From previous discussion,it can be seen that Using direct selection sort,in order to find out the smallest keyword among n keywords,n-1 comparisons are needed.Then n-2 comparisons are needed for finding out the minor keyword among the rest n-1 keywords.
In fact,in the n-2 comparisons to find out the minor keyword,there are many comparisons that may have been done in the previous n-1 comparisons.It just didn’t preserve the these results at that time.Therefore,these comparisons are repeated in the last sort.Tree sorting can overcome this.Heap sort is a tree type selection sort.Its basic idea is:
In the sorting process,the record array R[1…n] is regarded as a sequential storage structure of a complete binary tree.Using the internal relationship between parents and children in a complete binary tree,in the current unordered area,select the keyword maximum (or minimum) record.
Heap is defined as follows:
The keyword sequence k1,k2,…,kn of n records are called heap.
If and only if Ki ≤ K2i and Ki ≤ K2i+1 or Ki ≥ K2i and Ki ≥ K2i+1 .The former is called small root heap,the latter is called big root heap.For example,the keyword sequence (15,27,44,76,38,59) is a small root heap.You can adjust it to a big root heap(76,38,59,27,15,44).
Their corresponding complete binary trees are shown in the following figure:
Heap sorting uses the big root heap(or the smallest root heap) to select the record with the biggest(or smallest) keyword in the current unordered area to achieve sorting.
The operation of each sorting is as follows:
Adjust the current unordered area to a big root heap,and select the heap top record with the biggest keyword,exchange it with the last record in the unordered area.It is just contrary to selection sort.Heap sort is a process of constantly establishing heap.
Therefore,the key of heap sort is how to build a heap.Its specific practice is as follows:
- Store the keywords of file of waiting for sorting into array R[1…n].R is regarded as a storage structure of a complete binary tree.Each node stands for a record.The first record R[1] of source file serves as the root of binary tree.The following records R[2…n] successively layer by layer from left to right order arrange to build a complete binary tree.The left child of any node R[i] is R[2i], the right child is R[2i+1],parent is R[i/2]
- Suppose to build the big root heap:if the left subtree ,right subtree of some node i of a complete binary tree are already a heap,it just need to compare the big one of R[2i].key and R[2i+1].key with the R[i].key .If R[i].key is smaller,then exchange.But it could damage the heap of the next level.So continuously adopt the method above to build the heap of the next level.Until the subtree rooted with node i in the complete binary tree becomes a heap.
- The process is like a sieve.The smaller keywords are filtered down layer by layer.The bigger keywords are selected layer by layer.So this method of building a heap is called screening.
Algorithm of adjusting to a big root heap:
According the definition of heap and the building process above,we can know that the order number 1 node R[1](namely heap top) is the keyword biggest node among the n nodes in heap.Therefore,the process of heap sort is relatively simple.First exchange R[1] with R[n],make R[n] the node with the biggest keyword.Then,the nodes in R[1…n-1] are filtered.We gain R[1] as the biggest keyword node in the current unordered area.Then R[1] is exchanged with the last node R[n-1] in the current unordered area.Make R[n-1] the biggest keyword node.And so on,after n-1 exchange and screening,all nodes become incremental order,that is to say,the sorting ends.Therefore,heap sort algorithm is as follows:
边栏推荐
猜你喜欢
iMeta | Baidu certification is completed, search "iMeta" directly to the publisher's homepage and submission link
解决:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING
图文手把手教程--ESP32 一键配网(Smartconfig、Airkiss)
[论文阅读] Unpaired Image-to-Image Translation Using Adversarial Consistency Loss
高级转录组分析和R数据可视化火热报名中(2022.10)
Jina 实例秀|七夕神器!比你更懂你女友的AI口红推荐
Libtomcrypt AES 加密及解密
Jenkins使用手册(1) —— 软件安装
多媒体和物联网技术让版本“活”起来 129张黑胶唱片“百年留声”
Jina 实例秀|基于神经搜索的网络安全威胁检测(一)
随机推荐
nsq部署_andlua辅助源码
Win11如何隐藏输入法悬浮窗?
Digital management insight into retail and e-commerce operations - retail password
无代码平台描述文字入门教程
safe-point(safepoint 安全点) 和 safe-region(安全区域)「建议收藏」
物体颜色的来源
[论文翻译] Unpaired Image-to-Image Translation using Adversarial Consistency Loss
ThreadLocal详细分析
冰蝎工具开发实现动态二进制加密WebShell
昨夜梦佳人,七夕试伊妆丨基于ModelArts实现AI妆容迁移丨【玩转华为云】
Rust 入门指南 (用 WASM 开发第一个 Web 页面)
OD-Model【5】:YOLOv1
被Win11安全中心误删除的文件怎么恢复?
mysql进阶(二十六)MySQL 索引类型
Qt:小的任务管理器(task)
华为开源:聚焦开源基础软件,共建健康繁荣生态
Win11怎么进行左右键对调?
JS工厂模式_工厂模式进行封装
js文字转语音播报
CompletableFuture接口核心方法介绍