Skip to content

Commit 44b2b9c

Browse files
committed
add datastructure
1 parent a85d85e commit 44b2b9c

14 files changed

+86
-3
lines changed

Diff for: Search/binary_search.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def binary_search_while(arr, key):
2+
"""迭代版"""
3+
arr.sort()
4+
l_index, h_index = 0, len(arr) - 1
5+
while l_index <= h_index:
6+
mid = h_index - (h_index - l_index) // 2
7+
if key < arr[mid]:
8+
h_index = mid - 1
9+
elif key > arr[mid]:
10+
l_index = mid + 1
11+
else:
12+
return arr[mid]
13+
else:
14+
return
15+
16+
17+
def binary_search_re(arr, key):
18+
"""递归版"""
19+
arr.sort()
20+
21+
def binary_search_re_do(l_index, h_index):
22+
if l_index >= h_index:
23+
return arr[l_index]
24+
mid = h_index - (h_index - l_index) // 2
25+
if key < arr[mid]:
26+
return binary_search_re_do(l_index, mid - 1)
27+
elif key > arr[mid]:
28+
return binary_search_re_do(mid + 1, h_index)
29+
else:
30+
return arr[mid]
31+
32+
return binary_search_re_do(0, len(arr) - 1)

Diff for: Search/hash_search.py

Whitespace-only changes.

Diff for: Search/linear_search.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
def linear_search(arr, key):
22
"""顺序(线性)查找算法实现"""
3-
for index, value in enumerate(arr):
3+
for value in arr:
44
# 寻找目标
5-
if value == key:
6-
return index
5+
while value == key:
6+
return value

Diff for: Search/test_search.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from unittest import TestCase
2+
3+
4+
def get_random_arr(max_num, length):
5+
import random
6+
return [random.uniform(0, max_num) for x in range(length)]
7+
8+
9+
def create_arr_list():
10+
"""测试序列"""
11+
arr_list = [[0], [0, 1]]
12+
for x in range(6):
13+
arr = get_random_arr(10 ** x, 10 ** x)
14+
arr_list.append(arr)
15+
return arr_list
16+
17+
18+
def search_log(func):
19+
import time, random
20+
arr_list = create_arr_list()
21+
22+
def wrapper(*args, **kw):
23+
start = time.time()
24+
for arr in arr_list:
25+
key = random.choice(arr)
26+
func(arr=arr, key=key, *args, **kw)
27+
over = time.time()
28+
print(func.__name__ + ' time: %f' % (over - start))
29+
return
30+
31+
return wrapper
32+
33+
34+
class TestSearch(TestCase):
35+
@search_log
36+
def test_linear_search(self, arr, key):
37+
from Search.linear_search import linear_search
38+
if not key == linear_search(arr, key):
39+
self.fail()
40+
41+
@search_log
42+
def test_binary_search_while(self, arr, key):
43+
from Search.binary_search import binary_search_while
44+
if not key == binary_search_while(arr, key):
45+
self.fail()
46+
47+
@search_log
48+
def test_binary_search_re(self, arr, key):
49+
from Search.binary_search import binary_search_re
50+
if not key == binary_search_re(arr, key):
51+
self.fail()

Diff for: Sort/__pycache__/BubbleSort.cpython-35.pyc

-768 Bytes
Binary file not shown.

Diff for: Sort/__pycache__/CountSort.cpython-35.pyc

-540 Bytes
Binary file not shown.

Diff for: Sort/__pycache__/HeapSort.cpython-35.pyc

-778 Bytes
Binary file not shown.

Diff for: Sort/__pycache__/InsertSort.cpython-35.pyc

-484 Bytes
Binary file not shown.

Diff for: Sort/__pycache__/MargeSort.cpython-35.pyc

-744 Bytes
Binary file not shown.

Diff for: Sort/__pycache__/QuickSort.cpython-35.pyc

-1014 Bytes
Binary file not shown.

Diff for: Sort/__pycache__/SelectionSort.cpython-35.pyc

-466 Bytes
Binary file not shown.

Diff for: Sort/__pycache__/ShellSort.cpython-35.pyc

-499 Bytes
Binary file not shown.

Diff for: Sort/__pycache__/__init__.cpython-35.pyc

-201 Bytes
Binary file not shown.

Diff for: Sort/__pycache__/main.cpython-35.pyc

-1.49 KB
Binary file not shown.

0 commit comments

Comments
 (0)