排序算法——堆排序(Heap Sort)

排序算法——堆排序(Heap Sort)


堆(Heap)
Heap is a complete binary tree which satisfies the heap condition:
Each child has a key which is no greater than its parent’s.

There are some trees, which are heaps? which are non-heaps?
这里写图片描述
(There is power tool to draw Heap or other structure tree https://visualgo.net/en)
(a) is a heap.
(b) is not a heap as it is not a complete binary tree.
(c) is a heap. Please note that heap allows such nodes with identical key.
(d) is not a heap. 31 as a child is greater than its parent 26.
(e) is a heap.

Generally, we say a tree as a heap. Heap represents max-heap by default. On there other hand, we can have a min-heap in which each child is no smaller than its parent.

堆的数组表示(Heap as array)
这里写图片描述
这里写图片描述

We place its elements in level-order in an array H.
Assume the parent of X is a node of infinite key. Place it in H[0]. This is just for easily understanding. It actually does not exist.

堆的属性(Properties of heap)
1. H[i] has a parent H[i/2]. (1 ≤ i ≤ n)
2. H[i] ≤ H[i/2].
3. The root of the tree H[1] has the maximum key.
4. Each subtree is still a heap.
5. The nodes which are parents are in array positions 1 to ⌊n/2⌋. Why? because the last node is in position n. Its parent is in position ⌊n/2⌋. Obviously, nodes before n/2 are all parents.
6. The height of heap is ⌊log2 n⌋. Why? we assume that the tree has a height h and the number of nodes n. The number of nodes at lowest level is between 1 and 2^h. n is between
这里写图片描述
⌊X⌋ means get the integer part of X. For example,
这里写图片描述
Hence, h=⌊log2 n⌋.

堆的操作(Operations on heap)

1. Inject a new node.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值