Skip to content

Commit 2ac0266

Browse files
committed
二分查找实现
1 parent 0944866 commit 2ac0266

6 files changed

+36
-1
lines changed

Diff for: README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,15 @@
2626

2727
## Algorithms-Python
2828

29-
github trending,暂时没有发现中文版。
29+
来自 github trending ,暂时没有发现中文版。
3030

3131
[Algorithms-Python](https://github.com/TheAlgorithms/Python)
3232

33+
## Data Structures and Algorithms in Python
34+
[Data Structures and Algorithms in Python](https://doc.lagout.org/programmation/python/Data%20Structures%20and%20Algorithms%20in%20Python%20[Goodrich,%20Tamassia%20&%20Goldwasser%202013-03-18].pdf)
35+
36+
## 约定
37+
38+
本仓库中代码全部使用`Python3`实现,使用`Linux`操作系统运行。
39+
3340
>蜀之鄙有二僧,其一贫,其一富。贫者语于富者曰:“吾欲之南海,何如?”富者曰:“子何恃而往?”曰:“吾一瓶一钵足矣。”富者曰:“吾数年来欲买舟而下,犹未能也。子何恃而往?”越明年,贫者自南海还,以告富者。富者有惭色。

Diff for: binary_search/binary_search.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Created by imoyao at 2019/5/13 18:33
4+
5+
6+
def binary_search(sorted_lists, wanted_item):
7+
low_index = 0
8+
high_index = len(sorted_lists) - 1
9+
count = 0
10+
while low_index <= high_index:
11+
count += 1
12+
mid_index = (low_index + high_index) // 2
13+
# print(mid_index)
14+
mid_item = sorted_lists[mid_index]
15+
if mid_item == wanted_item:
16+
print('It takes {0} times to find {1} in index {2}'.format(count, wanted_item, mid_index))
17+
return mid_item
18+
else:
19+
if mid_item < wanted_item:
20+
low_index = mid_index + 1
21+
else:
22+
high_index = mid_index - 1
23+
return None
24+
25+
26+
if __name__ == '__main__':
27+
a = binary_search(list(range(100)), 50)
28+
print('ret:', a)
9.26 MB
Binary file not shown.

Diff for: static/sessdsa-textbook.epub

10.5 MB
Binary file not shown.

Diff for: static/sessdsa-textbook.pdf

8.82 MB
Binary file not shown.

Diff for: static/算法图解.pdf

16.9 MB
Binary file not shown.

0 commit comments

Comments
 (0)