File tree Expand file tree Collapse file tree 4 files changed +6
-5
lines changed
15_堆与堆排序/heap_and_heapsort Expand file tree Collapse file tree 4 files changed +6
-5
lines changed Original file line number Diff line number Diff line change 165
165
< li class ="toctree-l2 "> < a href ="#_5 "> 实现堆排序</ a > </ li >
166
166
167
167
168
- < li class ="toctree-l2 "> < a href ="#python-heapq "> python 里的 heapq</ a > </ li >
168
+ < li class ="toctree-l2 "> < a href ="#python-heapq "> Python 里的 heapq 模块 </ a > </ li >
169
169
170
170
171
171
< li class ="toctree-l2 "> < a href ="#top-k "> Top K 问题</ a > </ li >
@@ -351,7 +351,7 @@ <h1 id="_5">实现堆排序</h1>
351
351
assert heapsort_reverse(l) == sorted(l, reverse=True)
352
352
</ code > </ pre >
353
353
354
- < h1 id ="python-heapq "> python 里的 heapq</ h1 >
354
+ < h1 id ="python-heapq "> Python 里的 heapq 模块 </ h1 >
355
355
< p > python 其实自带了 heapq 模块,用来实现堆的相关操作,原理是类似的。请你阅读相关文档并使用内置的 heapq 模块完成堆排序。
356
356
一般我们刷题或者写业务代码的时候,使用这个内置的 heapq 模块就够用了。</ p >
357
357
< h1 id ="top-k "> Top K 问题</ h1 >
@@ -394,7 +394,7 @@ <h1 id="top-k">Top K 问题</h1>
394
394
395
395
def test():
396
396
import random
397
- i = list(range(1000))
397
+ i = list(range(1000)) # 这里可以是一个可迭代元素,节省内存
398
398
random.shuffle(i)
399
399
_ = TopK(i, 10)
400
400
print(_.get_topk())
@@ -410,6 +410,7 @@ <h1 id="_6">练习题</h1>
410
410
< li > 请你实现一个最小堆,你需要修改那些代码呢?</ li >
411
411
< li > 我们实现的堆排序是 inplace 的吗,如果不是,你能改成 inplace 的吗?</ li >
412
412
< li > 堆排序的时间复杂度是多少? siftup 和 siftdown 的时间复杂度是多少?(小提示:考虑树的高度,它决定了操作次数)</ li >
413
+ < li > 请你思考 Top K 问题的时间复杂度是多少?</ li >
413
414
</ ul >
414
415
< h1 id ="_7 "> 延伸阅读</ h1 >
415
416
< ul >
Original file line number Diff line number Diff line change @@ -581,5 +581,5 @@ <h2 id="_21">本电子书制作和写作方式</h2>
581
581
582
582
<!--
583
583
MkDocs version : 1.0.4
584
- Build Date UTC : 2018-12-23 02:56:52
584
+ Build Date UTC : 2018-12-23 03:03:35
585
585
-->
Load Diff Large diffs are not rendered by default.
You can’t perform that action at this time.
0 commit comments