Fancy DSA Fancy DSA
数据结构
算法
LeetCode
  • 关于
  • 导航 (opens new window)
  • 分类
  • 标签
  • 归档
设计模式 (opens new window)
博客 (opens new window)
GitHub (opens new window)

Jonsam NG

想的更多,也要想的更远
数据结构
算法
LeetCode
  • 关于
  • 导航 (opens new window)
  • 分类
  • 标签
  • 归档
设计模式 (opens new window)
博客 (opens new window)
GitHub (opens new window)
  • 开始上手
  • Plan 计划
  • Roadmap 路线
  • 算法简介
  • Sort 排序

    • 章节概要
    • AlphaNumericalSort [字母顺序排序]
    • BeadSort [珠排序]
    • BogoSort [Bogo 排序]
    • BubbleSort [冒泡排序]
    • BucketSort [桶排序]
    • CocktailShakerSort [鸡尾酒排序]
    • CombSort [梳排序]
    • CountingSort [计数排序]
    • CycleSort [圈排序]
    • FisherYatesShuffle [洗牌算法]
    • FlashSort [闪电排序]
    • GnomeSort [侏儒排序]
    • HeapSort [堆排序]
      • 介绍
      • 原理
      • 复杂度
      • 动画
      • 实现
      • 参考
    • InsertionSort [插入排序]
    • IntroSort [内省排序]
    • MergeSort [归并排序]
    • OddEvenSort [奇偶排序]
    • PancakeSort [煎饼排序]
    • PigeonHoleSort [鸽巢排序]
    • QuickSort [快速排序]
    • RadixSort [基数排序]
    • SelectionSort [选择排序]
    • ShellSort [希尔排序]
    • TimSort [Tim 排序]
    • TopologicalSorter [拓扑排序器]
    • WiggleSort [摆动排序]
  • Search 搜索

  • Recursive 递归

  • Graph 图

  • Tree 树

  • Math 数学

  • Hash 哈希

  • String 字符串

  • BitManipulation 位操纵

  • Backtracking 回溯

  • DynamicProgramming 动态规划

  • Cache 缓存

  • Array 数组

  • Ciphers 密码学

  • Conversions 转换

  • ProjectEuler 欧拉计划

  • 其他

  • 算法
  • Sort 排序
jonsam
2022-04-26
目录

HeapSort [堆排序]

# 介绍

堆排序(英语:HeapSort)是指利用堆这种数据结构所设计的一种排序算法。堆是一个近似完全二叉树的结构,并同时满足堆的性质:即子节点的键值或索引总是小于(或者大于)它的父节点。

# 原理

若以升序排序说明,把数组转换成最大堆 (Max-Heap Heap),这是一种满足最大堆性质 (Max-Heap Property) 的二叉树:对于除了根之外的每个节点 i, A[parent(i)]≥A[i]A[parent(i)] ≥ A[i]A[parent(i)]≥A[i]。

重复从最大堆取出数值最大的结点 (把根结点和最后一个结点交换,把交换后的最后一个结点移出堆),并让残余的堆维持最大堆性质。

Sorting_heapsort_anim.gif (280×214)

堆排序的基本思路:

  • 将无需序列构建成一个堆,根据升序降序需求选择大顶堆或小顶堆;
  • 将堆顶元素与末尾元素交换,将最大元素 "沉" 到数组末端;
  • 重新调整结构,使其满足堆定义,然后继续交换堆顶元素与当前末尾元素,反复执行调整 + 交换步骤,直到整个序列有序。

# 堆节点访问

通常堆是通过一维数组来实现的。在数组起始位置为 0 的情形中:

  • 父节点 i 的左子节点在位置 (2i+1){\displaystyle (2i+1)}(2i+1);
  • 父节点 i 的右子节点在位置 (2i+2){\displaystyle (2i+2)}(2i+2);
  • 子节点 i 的父节点在位置 ⌊(i−1)/2⌋{\displaystyle \lfloor (i-1)/2\rfloor }⌊(i−1)/2⌋;

# 复杂度

  • 平均时间复杂度 Θ(nlog⁡n)\Theta (n\log n)Θ(nlogn)
  • 最坏时间复杂度 O(nlog⁡n)O(n\log n)O(nlogn)
  • 最优时间复杂度 O(nlog⁡n)O(n\log n)O(nlogn)
  • 空间复杂度 O(n)O(n)O(n) total, O(1)O(1)O(1) auxiliary

# 动画

# 实现

# JavaScript

/*
 * Build a max heap out of the array. A heap is a specialized tree like
 * data structure that satisfies the heap property. The heap property
 * for max heap is the following: "if P is a parent node of C, then the
 * key (the value) of node P is greater than the key of node C"
 * Source: https://en.wikipedia.org/wiki/Heap_(data_structure)
 */
/* eslint no-extend-native: ["off", { "exceptions": ["Object"] }] */
Array.prototype.heapify = function (index, heapSize) {
  let largest = index
  const leftIndex = 2 * index + 1
  const rightIndex = 2 * index + 2

  if (leftIndex < heapSize && this[leftIndex] > this[largest]) {
    largest = leftIndex
  }

  if (rightIndex < heapSize && this[rightIndex] > this[largest]) {
    largest = rightIndex
  }

  if (largest !== index) {
    const temp = this[largest]
    this[largest] = this[index]
    this[index] = temp

    this.heapify(largest, heapSize)
  }
}

/*
 * Heap sort sorts an array by building a heap from the array and
 * utilizing the heap property.
 * For more information see: https://en.wikipedia.org/wiki/Heapsort
 */
function heapSort (items) {
  const length = items.length

  for (let i = Math.floor(length / 2) - 1; i > -1; i--) {
    items.heapify(i, length)
  }
  for (let j = length - 1; j > 0; j--) {
    const tmp = items[0]
    items[0] = items[j]
    items[j] = tmp
    items.heapify(0, j)
  }
  return items
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

# 参考

  • 堆 - 维基百科,自由的百科全书 (opens new window)
  • 堆排序 - 维基百科,自由的百科全书 (opens new window)
  • 图解排序算法 (三) 之堆排序 (opens new window)
编辑 (opens new window)
上次更新: 2022/10/10, 21:03:42
GnomeSort [侏儒排序]
InsertionSort [插入排序]

← GnomeSort [侏儒排序] InsertionSort [插入排序]→

最近更新
01
0-20题解
10-31
02
本章导读
10-31
03
算法与转换:Part1
10-28
更多文章>
Theme by Vdoing | Copyright © 2022-2022 Fancy DSA | Made by Jonsam by ❤
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式