Skip to content

Commit c47f3c8

Browse files
committed
Deployed 4e0c3a8 with MkDocs version: 1.0.4
1 parent cb2095c commit c47f3c8

30 files changed

+46
-4
lines changed

.DS_Store

6 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

03_链表/wnntest.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
# python linked_list.py
4+
when-changed . 'py.test -s linked_list.py'
5+
# when-changed . 'py.test -s double_link_list.py'
6+
# python double_link_list.py
Binary file not shown.
Binary file not shown.
Binary file not shown.
8.47 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

13_高级排序算法/quick_sort/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ <h1 id="_3">思考题</h1>
321321
<li>我们实现的快排是稳定的啵?</li>
322322
<li>选择基准值如果选不好就可能导致复杂度升高,算导中提到了一种『median of 3』策略,就是说选择 pivot 的时候
323323
从子数组中随机选三个元素,再取它的中位数,你能实现这个想法吗?这里我们的代码很简单地取了第一个元素作为 pivot</li>
324-
<li>利用快排中的 partition 操作,我们还能实现另一个算法,nth_element,快速查找一个无序数组中的第 k 大元素,请你尝试实现它并编写单测</li>
324+
<li>利用快排中的 partition 操作,我们还能实现另一个算法,nth_element,快速查找一个无序数组中的第 n 大元素,请你尝试实现它并编写单测。其实这个函数是 C++ STL 中的一个函数。</li>
325325
<li>你知道 Python 内置的 sorted 如何实现的吗?请你 Google 相关资料了解下。很多内置的排序都使用了快排的改良版。</li>
326326
</ul>
327327
<h1 id="_4">延伸阅读</h1>

13_高级排序算法/quicksort.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def quicksort_inplace(array, beg, end): # 注意这里我们都用左闭右
2727

2828

2929
def partition(array, beg, end):
30+
"""对给定数组执行 partition 操作,返回新的 pivot 位置"""
3031
pivot_index = beg
3132
pivot = array[pivot_index]
3233
left = pivot_index + 1
@@ -55,7 +56,7 @@ def test_partition():
5556
l = [1, 2, 3, 4]
5657
assert partition(l, 0, len(l)) == 0
5758
l = [4, 3, 2, 1]
58-
assert partition(l, 0, len(l))
59+
assert partition(l, 0, len(l)) == 3
5960

6061

6162
def test_quicksort_inplace():
@@ -65,3 +66,31 @@ def test_quicksort_inplace():
6566
sorted_seq = sorted(seq)
6667
quicksort_inplace(seq, 0, len(seq))
6768
assert seq == sorted_seq
69+
70+
71+
def nth_element(array, beg, end, nth):
72+
"""查找一个数组第 n 大元素"""
73+
if beg < end:
74+
pivot_idx = partition(array, beg, end)
75+
if pivot_idx == nth - 1: # 数组小标从 0 开始
76+
return array[pivot_idx]
77+
elif pivot_idx > nth - 1:
78+
return nth_element(array, beg, pivot_idx, nth)
79+
else:
80+
return nth_element(array, pivot_idx + 1, end, nth)
81+
82+
83+
def test_nth_element():
84+
l1 = [3, 5, 4, 2, 1]
85+
assert nth_element(l1, 0, len(l1), 3) == 3
86+
assert nth_element(l1, 0, len(l1), 2) == 2
87+
88+
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
89+
for i in l:
90+
assert nth_element(l, 0, len(l), i) == i
91+
for i in reversed(l):
92+
assert nth_element(l, 0, len(l), i) == i
93+
94+
95+
if __name__ == '__main__':
96+
test_nth_element()

13_高级排序算法/test.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
3+
#!/usr/bin/env bash
4+
5+
# pip install when-changed, 监控文件变动并且文件修改之后自动执行 pytest 单测,方便我们边修改边跑测试
6+
when-changed -v -r -1 -s ./ "py.test -s $1"
7+
Binary file not shown.
Binary file not shown.
Binary file not shown.

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,5 +528,5 @@ <h2 id="_20">本电子书制作和写作方式</h2>
528528

529529
<!--
530530
MkDocs version : 1.0.4
531-
Build Date UTC : 2018-12-19 00:37:20
531+
Build Date UTC : 2018-12-19 13:43:53
532532
-->

search/search_index.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

sitemap.xml.gz

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)