惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
V
V2EX
L
LangChain Blog
WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
T
The Blog of Author Tim Ferriss
Recorded Future
Recorded Future
月光博客
月光博客
Y
Y Combinator Blog
Stack Overflow Blog
Stack Overflow Blog
美团技术团队
博客园 - 【当耐特】
The Cloudflare Blog
罗磊的独立博客
GbyAI
GbyAI
A
About on SuperTechFans
腾讯CDC
宝玉的分享
宝玉的分享
I
InfoQ
V
Visual Studio Blog
Forbes - Security
Forbes - Security
P
Proofpoint News Feed
T
Troy Hunt's Blog
NISL@THU
NISL@THU
Webroot Blog
Webroot Blog
T
Threatpost
博客园 - 三生石上(FineUI控件)
S
Securelist
H
Help Net Security
小众软件
小众软件
L
Lohrmann on Cybersecurity
Cyberwarzone
Cyberwarzone
T
The Exploit Database - CXSecurity.com
量子位
博客园_首页
Scott Helme
Scott Helme
酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
J
Java Code Geeks
G
GRAHAM CLULEY
T
Tor Project blog
The GitHub Blog
The GitHub Blog
Cloudbric
Cloudbric
IT之家
IT之家
阮一峰的网络日志
阮一峰的网络日志
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Schneier on Security
Schneier on Security
V
Vulnerabilities – Threatpost
Jina AI
Jina AI

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN Mintty Tips and Configurations Generating Table of Contents for Markdown with Tagbar Convert Python Script to Exe on Windows with Pyinstaller Ubuntu on Windows Missing after Windows Update 使用代理加速 Mac 终端下载速度 My Experience with Several Zsh Plugin Managers 深圳租房小记 How to Install zplug inside Docker Container Why don't settings inside bashrc or bash_profile take effect? Setting Up Locale in Linux 谷歌 Adsense 申请及在 Hugo 中的配置 How to Write Algorithm Pseudo Code in LaTeX Nifty Nvim Techniques That Make My Life Easier -- Series 4 A Few Grammar Questions in Writing How to Read and Write Images with Unicode Paths in OpenCV Creating A Professional Table in LaTeX with booktabs How to Create Proper Folding for Vim/Nvim Configuration Linux Tips and Tricks -- s1 JPEG Image Orientation and Exif How Do I Show the Current File Path In Neovim? JPEG Image Quality in PIL Difference between view, reshape, transpose and permute in PyTorch Convert PIL or OpenCV Image to Bytes without Saving to Disk Fast Movement and Navigation Inside Vim or Neovim Unintuitive Behaviour of Case Sensitivity in Python glob Binding Keys in Zsh 几把机械键盘试用体验 Nvim Autocompletion with Deoplete Converting Markdown to Beautiful PDF with Pandoc Exclusive and Inclusive Motion in Neovim/Vim Nifty Nvim Techniques Which Make My Life Easier -- Series 3 Why Doesn't Jedi Autocompletion Work for Some Methods Vim-like Editing inside Browser Markdown 生成 HTML 时汉字之间出现多余空格问题 小米 9 安装谷歌商店(Google Play Store)与相关配置 Create Mappings That Take A Count in Neovim Spell Checking in Nvim English Words Completion inside Neovim/Vim How to Use Python Inside Vim Script with Neovim Nifty Little Nvim Techniques to Make My Life Easier -- Series 2 Setting up Ultisnips for Neovim Mac 上罗技 M590 鼠标设置 Nifty Little Nvim Techniques to Make My Life Easier -- Series 1 A Complete Guide on Writing LaTeX with Vimtex in Neovim Manipulating Images with Alpha Channels in Pillow Sublime Text Regular Expression Cheat Sheet Cropping Rotated Rectangles from Image with OpenCV Boosting Your Productivity on Terminal with Zsh and Plugins 最新版 Rime 输入法使用 (2022 更新) Display Image with Pillow inside Ubuntu on Windows Faster Directory Navigation with z.lua Cmder Advanced Configurations Nvim-qt Settings on Windows 10 Tmux Plugin Install and Management How to Debug Python Code in Terminal Markdown Writing and Previewing in Neovim -- A Complete Guide Line Number Settings for More Efficient Movement in Neovim 两个大规模中文语料库介绍以及处理 Windows 系统下几款程序员不可不用的神器 我的 2018 阅读清单 A Complete Guide to Neovim Configuration for Python Development How Is Newline Handled in Python and Various Editors? Two Issues Related to ImageFont Module in PIL 在 Listary 中调用 GoldenDict 或欧路词典查单词 Reading and Writing Text Files on Windows The Mathematics behind Font Shapes --- Bézier Curves and More 快速识别图片字体:字体识别工具介绍 Deoplete Failed to Load at Startup after Updating Python neovim Package What Is The Difference between pip, pip3 and pip3.6 Shipped with Anaconda3? Windows 10 系统下 Neovim 安装与配置
堆排序是如何工作的?
2017-10-06 · via jdhao's digital space

本文将介绍堆 (heap)的概念,进一步介绍如何把普通数组变成最大堆,并在此基础上进行堆排序。

基础概念

要谈堆排序,必须先说清楚“堆”这种数据结构,堆的定义如下(来自维基百科):

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of node P is greater than the key of node C. A heap can be classified further as either a “max heap” or a “min heap”. In a max heap, the keys of parent nodes are always greater than or equal to those of the children and the highest key is in the root node. In a min heap, the keys of parent nodes are less than or equal to those of the children and the lowest key is in the root node.

在本文中,我们讨论的均是最大堆(max heap)。堆是一种完全二叉树(complete binary tree),所谓完全二叉树,就是满二叉树 (full binary tree)去掉最后一层右边的一些节点构成的二叉树。关于二者的定义,可以参考这里。下面是一张形象的表示图,

我们可以按照从上到下,从左到右的顺序给堆的节点编号,根节点编号为 \(0\),如上图中右图所示(注意上图并不是一个最大堆或者最小堆,仅仅是示意可以这样来给堆的节点编号。这样的话,堆可以用一个数组 \(A\) 来表示,\(A[0]\) 表示堆的根节点的值。 由于堆是完全二叉树,计算编号为 \(m\) 的节点的父节点以及左右子节点(如果左右子节点存在的话)非常容易,计算公式由以下式子给出,

父节点:\(\lfloor \frac{m-1}{2}\rfloor\) 左子节点:\(2m + 1\) 右子节点:\(2m + 2\)

证明如下,假设根节点所在的层为第 \(0\) 层,那么第 \(i\) 层节点个数为 \(2^i\) 个, 第 \(i\) 层各个节点编号为 \(2^i - 1\)(第 1 个节点),\(2^i + k - 2\)(第 k 个节点),\(2^{i+1} - 2\)(第 \(2^i\) 个节点)。对于第 \(i\) 层第 \(k\) 个节点(编号是 \(2^i + k - 2\)),其在下层的左子节点编号计算公式为

\[\begin{equation}\begin{aligned} \text{idLeftChild} &= \left [ 2^i + k - 2 - (2^i - 1)\right ] + 2^{i+1} - 1\\ &= 2k - 2 + 2^{i+1} - 1 \\ &= 2^{i+1} + 2k - 4 +1 \\ &= \left (2^i + k - 2 \right )*2 + 1 \end{aligned}\end{equation}\]

从以上公式可以归纳出,编号为 \(m\) 的节点,其左子节点编号为 \(2m+1\),右子节点编号为 \(2m+2\),其父节点编号为 \(\lfloor \frac{m-1}{2}\rfloor\)

任意普通数组的“堆化”

因为完全二叉树可以用数组表示,反过来任意的数组也对应一个完全二叉树,只是这个完全二叉树不一定是最大堆。 给定一个数组,如何把它变为一个堆呢?观察一个最大堆,我们会发现,除了叶子节点,其余每个节点都满足最大堆的特点,因此要把一个数组对应的完全二叉树变为最大堆,只需要自底向上,逐层把各个非叶子节点逐一最大堆化即可,同时要保证最大堆的特性在最大堆化过程中不被破坏。

对于大小为 \(n\) 的数组 \(A\),很容易看出从节点 \(0\) 到节点 \(\lfloor \frac{n}{2} - 1\rfloor\) 含有叶子节点,其余节点均为非叶子节点,所以最大堆化的过程只需要对这些非叶子节点进行。下面是最大堆化的 C++ 实现,

void max_heapify(vector<int>& arr, const int N, int i){
    int largest = i;
    int l = 2*i + 1;
    int r = 2*i + 2;
    if (l < N && arr[l] > arr[largest])
        largest = l;
    if (r < N && arr[r] > arr[largest])
        largest = r;

    // if largest val is not in parent node, we need to
    // switch val of parent node and largest val
    if (largest != i){
        swap(arr[i], arr[largest]);
        max_heapify(arr, N, largest);
    }
}

void make_heap(vector<int>& arr, const int N){
    for (int i = N/2 -1; i >= 0; --i){
        max_heapify(arr, N, i);
    }
}

堆排序的思想与实现

对于的给定的数组,堆排序算法工作原理如下

  1. 把当前的无序数组变为最大堆(arr[0] 为数组最大元素)
  2. 对于 i 从 n-1 到 1,
    1. 交换 arr[0] 与当前数组的最后一个元素的位置 arr[i-1],然后把堆的大小减 1
    2. 把剩余的二叉树的根节点重新变为最大堆化(因为该二叉树只有根节点不满足最大堆的条件

把以上算法写成代码如下

void heap_sort(vector<int>& arr){
    const int N = static_cast<int>(arr.size());
    make_heap(arr, N);

    for (int i = N-1; i >= 1; --i){
        swap(arr[0], arr[i]);
        max_heapify(arr, i, 0);
    }
}

下面是一个完整的可以运行的例子,

#include <bits/stdc++.h>
using namespace std;
void printArray(const vector<int>& arr){
    for(auto& num: arr){
        cout << num << " ";
    }
    cout << endl;
}

int main(){
    const int N = 10;
    const int rangeBegin = 0;
    const int rangeEnd = 1000;

    random_device rd;
    mt19937 gen(rd());
    uniform_int_distribution<int> distribution(rangeBegin, rangeEnd);

    vector<int> arr(N, 0);
    for (int i = 0; i != N; ++i){
        arr[i] = distribution(gen);
    }

    cout << "Original array: ";
    printArray(arr);

    heap_sort(arr);
    cout << "After heap sort: ";
    printArray(arr);


    return 0;
}

示例运行结果如下:

时间复杂度,空间复杂度及稳定性

构建最大堆的时间复杂度为 \(O(n)\) (并不是 \(O(\log(n))\),详细论证过程见这里),heap sort 时间复杂度为\(O(n\log(n))\),因此总的时间复杂度为 \(O(n\log(n))\),由于堆排序只使用了常量空间,因此该算法空间复杂度是 \(O(1)\)

前面的博客也提到过,排序算法的稳定性在某些情况下很重要. 本文中的堆排序属于不稳定的算法,举例来说明,假设输入的数组是 {2a, 2b, 1} (a, b 用来区分相同值的先后顺序),这个数组已经是最大堆了,使用 heap sort 算法,得到的排序后的结果应该是 {1, 2b, 2a},相同数值的先后顺序被破坏,这就说明了堆排序是不稳定的算法。


References